Custom form error message - aria

Is it possible to modify the form’s own error message so that some parameters are added?

<form>
	<label for="name">Name</label>
	<input name="name" id="name" type="text" required>
	<div class="error-message">Fill the name</div>
</form>

I want it to be

<form>
	<label for="name">Name</label>
	<input name="name" id="name" type="text" aria-required="true" aria-invalid="true" aria-describedby="hint-name" required>
	<div class="error-message" id="hint-name">Fill the name</div>
</form>

It is important to put the ID in the error-message, for aria.

FormHelper contain template:

'error' => '<div class="error-message">{{content}}</div>',

Thank you for help

You should look at Form - 4.x

See cakephp/FormHelper.php at master · cakephp/cakephp · GitHub for all the templates/keys which can be overwritten.

The problem you are running into is the fact, that the default error template doesn’t have any other “variables” which can be used inside the template.

This line only sets the content variable which then will be output at the position of {{content}} in this template.

So in your example you would have to extend the FormHelper with a custom class, overwrite the error function and add that functionality in there.

I haven’t thought it completely through but those are the code parts you need to start looking at to get to know how this whole system works.

1 Like

Thank you, and how can you overwrite the error function, can you please give an example?

look at forms - cakephp 3: change class input error - Stack Overflow

1 Like