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

Form error() does not display field error #16165

Closed
dereuromark opened this issue Dec 2, 2021 · 7 comments
Closed

Form error() does not display field error #16165

dereuromark opened this issue Dec 2, 2021 · 7 comments

Comments

@dereuromark
Copy link
Member

dereuromark commented Dec 2, 2021

Description

$field = 'flag_required';
...
<?php echo $this->Form->create($bitmaskedRecord);?>
	<fieldset>
 		<legend><?php echo __('Add {0}', __('Entry')); ?></legend>
	<?php
		echo $this->Form->control('name');
		echo $this->Form->control('flags', ['type' => 'select', 'multiple' => 'checkbox']);

		// For another field we explicitly output the error here if applicable
		echo $this->Form->error($field);
	?>
	</fieldset>
<?php echo $this->Form->submit(__('Submit'));
echo $this->Form->end();?>

Content of the $bitmaskedRecord->getErrors() error is

^ array:1 [▼
  "flag_required" => array:1 [▼
    "_empty" => "This field cannot be left empty"
  ]
]

The live demo and code can be seen here:
https://sandbox.dereuromark.de/sandbox/tools-examples/bitmasks?required=1
Locally checked out it can also be reproduced directly via sandbox.local URL

So it is definitely there, and it used to work in 2.x/3.x days afaik.

CakePHP Version

4.x

What is super weird
Once I add

$bitmaskedRecord->setError('flags', $bitmaskedRecord->getError($field));

in the controller to kind of force the the flags field directly to be red, it displays the error twice (then suddenly the call works).
Makes me think the FormHelper has an issue accessing errors until an actual field in use did it.

The fix that kinda works is here: dereuromark/cakephp-sandbox#45
But for me this is more a workaround.

@dereuromark dereuromark added this to the 4.3.3 milestone Dec 2, 2021
@ADmad
Copy link
Member

ADmad commented Dec 2, 2021

What's a "mapped field"? Why do you expect errors set for flag_required field to show up for flags field?

@dereuromark
Copy link
Member Author

dereuromark commented Dec 2, 2021

Why do you expect errors set for flag_required field to show up for flags field?

I don't, thats why I added an explicit standalone error() call below :)

As for the reason why, my hunch seems to be correct
When you post the form with a name added, it will not invalidate the name, so the custom error() is the first call.
If you enter empty spaces, it will invalidate the name, and with it the custom error() also shows up for the unrelated field flag_required (which is the error for our empty checkboxes that should invalidate in both cases).

@ADmad
Copy link
Member

ADmad commented Dec 2, 2021

Ah so the error for the non-entity field flag_required only shows up when one of the entity fields also has an error?

@dereuromark
Copy link
Member Author

Those are both entity fields, but only of of them is displayed in the form using control().

@dereuromark
Copy link
Member Author

dereuromark commented Dec 2, 2021

I debugged it further and it seems the error() actually returns

    return $this->formatTemplate('error', [
        'content' => $error,
        'id' => $this->_domId($field) . '-error',
    ]);

with

CORE/src/View/Helper/FormHelper.php (line 772)
'<div class="invalid-feedback">This field cannot be left empty</div>'

So it is actually there, but with display:none
Which means it is just not visible/displayed properly.

Once another control() related error shows up, the CSS rule makes it appear:

.was-validated :invalid ~ .invalid-feedback,
.was-validated :invalid ~ .invalid-tooltip,
.is-invalid ~ .invalid-feedback,
.is-invalid ~ .invalid-tooltip {
  display: block;
}

So I guess this boils down to error() also counting in the result of the form itself to be displayed as erroring to make validation errors visible.
=> Giving it the is-invalid main CSS class.

@markstory markstory self-assigned this Dec 4, 2021
@markstory
Copy link
Member

I took a look at the sandbox page, and the form error is in the page. It looks like your CSS is hiding the error messages though.

The form control for flags doesn't have the error so it isn't marked as invalid. All of this behavior is working as intended so I don't know what needs to be fixed in Cake.

@markstory markstory removed their assignment Dec 5, 2021
@dereuromark
Copy link
Member Author

Error standalone should not be hidden, same as other fields. But I dont have a good solution either.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants