Add class on form field error

blackcat562
Member | 10
+
0
-

Is there a way to add a class to a form field when there's an error, such as it being empty?

I am using standalone forms

Marek Bartoš
Nette Blogger | 1275
+
+1
-

If you are rendering form manually (not using renderer) then use $input->hasErrors()

blackcat562
Member | 10
+
0
-

Marek Bartoš wrote:

If you are rendering form manually (not using renderer) then use $input->hasErrors()

I tried that but it doesn't seem to do anything, I have a field called ‘name’ defined like this:
$form->addText('name', 'Your Name:')->setRequired('Required: Name');

and if I try to following on my form template, when it's empty, it doesn't do anything:
$form['name']->hasErrors()

if I do a var_dump($form['name']->hasErrors()) I get bool(false), even though the field is empty, so there should be an error

Last edited by blackcat562 (2022-10-04 22:54)

Marek Bartoš
Nette Blogger | 1275
+
0
-

Did you submit the form? This should work just fine

blackcat562
Member | 10
+
0
-

I tried it, but like I said it didn't work. Is there something I need to do to have nette.js apply the error class to this fields?