Skip to content

Commit

Permalink
Fix error messages not being displayed with nested input elements.
Browse files Browse the repository at this point in the history
The error message element is not being displayed when it's not a sibling
of an (input) element with an `is-invalid` class, which is the case for:

* Input groups
* Horizontally aligned checkboxes
* Custom file controls
  • Loading branch information
ndm2 committed May 29, 2020
1 parent 38ec5c3 commit 1270a40
Show file tree
Hide file tree
Showing 3 changed files with 2,316 additions and 97 deletions.
27 changes: 19 additions & 8 deletions src/View/Helper/FormHelper.php
Expand Up @@ -89,10 +89,10 @@ class FormHelper extends Helper
'multicheckboxWrapper' => '<fieldset class="form-group">{{content}}</fieldset>',
'multicheckboxTitle' => '<legend class="col-form-label pt-0">{{text}}</legend>',
'customFileLabel' => '<label class="custom-file-label"{{attrs}}>{{text}}{{tooltip}}</label>',
'customFileFormGroup' => '<div class="custom-file">{{input}}{{label}}</div>',
'customFileFormGroup' => '<div class="custom-file {{invalid}}">{{input}}{{label}}</div>',
'customFileInputGroupFormGroup' => '{{input}}',
'customFileInputGroupContainer' => '<div{{attrs}}>{{prepend}}<div class="custom-file">{{content}}{{label}}' .
'</div>{{append}}</div>',
'customFileInputGroupContainer' =>
'<div{{attrs}}>{{prepend}}<div class="custom-file {{invalid}}">{{content}}{{label}}</div>{{append}}</div>',
'nestingLabel' => '{{hidden}}{{input}}<label{{attrs}}>{{text}}{{tooltip}}</label>',
'nestingLabelNestedInput' => '{{hidden}}<label{{attrs}}>{{input}}{{text}}{{tooltip}}</label>',
];
Expand Down Expand Up @@ -127,13 +127,13 @@ class FormHelper extends Helper
'label' => '<label class="col-form-label %s"{{attrs}}>{{text}}{{tooltip}}</label>',
'fileLabel' => '<label class="col-form-label pt-1 %s"{{attrs}}>{{text}}{{tooltip}}</label>',
'formGroup' => '{{label}}<div class="%s">{{input}}{{error}}{{help}}</div>',
'customFileFormGroup' => '<div class="%s"><div class="custom-file">{{input}}{{label}}</div>' .
'{{error}}{{help}}</div>',
'customFileFormGroup' =>
'<div class="%s"><div class="custom-file {{invalid}}">{{input}}{{label}}</div>{{error}}{{help}}</div>',
'customFileInputGroupFormGroup' => '<div class="%s">{{input}}{{error}}{{help}}</div>',
'checkboxFormGroup' => '<div class="%s"><div class="form-check">{{input}}{{label}}</div>' .
'{{error}}{{help}}</div>',
'checkboxFormGroup' =>
'<div class="%s"><div class="form-check">{{input}}{{label}}{{error}}{{help}}</div></div>',
'customCheckboxFormGroup' => '<div class="%s"><div class="custom-control custom-checkbox">' .
'{{input}}{{label}}</div>{{error}}{{help}}</div>',
'{{input}}{{label}}{{error}}{{help}}</div></div>',
'datetimeContainer' => '<div class="form-group row {{type}}{{required}}" role="group" ' .
'aria-labelledby="{{groupId}}">{{content}}</div>',
'datetimeContainerError' => '<div class="form-group row {{type}}{{required}} is-invalid" ' .
Expand Down Expand Up @@ -450,6 +450,10 @@ public function control(string $fieldName, array $options = []): string
$options['templates']['label'] = $this->templater()->get('customFileLabel');
$options['templates']['formGroup'] = $this->templater()->get('customFileFormGroup');

if ($this->_getContext()->hasError($fieldName)) {
$options['templateVars']['invalid'] = $this->_config['errorClass'];
}

if (
$options['prepend'] ||
$options['append']
Expand Down Expand Up @@ -512,6 +516,13 @@ public function control(string $fieldName, array $options = []): string
unset($options['tooltip']);
}

if (
isset($options['append']) ||
isset($options['prepend'])
) {
$options['injectErrorClass'] = $this->_config['errorClass'];
}

$result = parent::control($fieldName, $options);

if ($newTemplates) {
Expand Down
11 changes: 10 additions & 1 deletion src/View/Widget/InputgroupTrait.php
Expand Up @@ -33,6 +33,7 @@ protected function _withInputGroup(array $data, ContextInterface $context): stri
'prepend' => null,
'append' => null,
'injectFormControl' => true,
'injectErrorClass' => null,
'input' => null,
];

Expand All @@ -42,7 +43,8 @@ protected function _withInputGroup(array $data, ContextInterface $context): stri

$prepend = $data['prepend'];
$append = $data['append'];
unset($data['append'], $data['prepend'], $data['injectFormControl']);
$errorClass = $data['injectErrorClass'];
unset($data['append'], $data['prepend'], $data['injectFormControl'], $data['injectErrorClass']);

if (isset($data['input'])) {
$input = $data['input'];
Expand All @@ -68,6 +70,13 @@ protected function _withInputGroup(array $data, ContextInterface $context): stri
}

if ($prepend || $append) {
if (
$errorClass &&
$context->hasError($data['fieldName'])
) {
$attrs['class'][] = $errorClass;
}

$input = $this->_templates->format('inputGroupContainer', [
'attrs' => $this->_templates->formatAttributes($attrs),
'append' => $append,
Expand Down

0 comments on commit 1270a40

Please sign in to comment.