Skip to content

Commit

Permalink
Merge pull request #314 from ndm2/fix/error-messages
Browse files Browse the repository at this point in the history
Fix error messages not being displayed with nested input elements
  • Loading branch information
ADmad committed May 31, 2020
2 parents 35b83c0 + 77acb9e commit f3eeb49
Show file tree
Hide file tree
Showing 3 changed files with 2,316 additions and 99 deletions.
29 changes: 19 additions & 10 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 @@ -315,8 +315,6 @@ public function control(string $fieldName, array $options = []): string
case 'datetime':
case 'date':
case 'time':
$options['hasError'] = $this->_getContext()->hasError($fieldName);

$options['label']['templateVars']['groupId'] =
$options['templateVars']['groupId'] =
$this->_domId($fieldName . '-group-label');
Expand Down Expand Up @@ -452,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 @@ -514,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 f3eeb49

Please sign in to comment.