Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error messages not being displayed with nested input elements #314

Merged
merged 2 commits into from May 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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