I want to add red border around Input Text Box when ZF2 Validation failed. I am thinking if ZF2 provide way to add/set css class in case of validation failure. It is better if it is possible using only Zend Validation/Form API. I am not considering client side (javascript/jQuery) solutions.
UPDATE :
I am using Form Row element like that
<div class="row">
<div><?php echo $this->formLabel($form->get('first_name')); ?></div>
<div><?php echo $this->formElement($form->get('first_name')); ?></div>
<div><?php echo $this->formElementErrors($form->get('first_name'), array('class' => "field-validation-error")); ?></div>
</div>
If you wish to add specific 'error' classes to the input will need to modify the related
Zend\Form\View\Helper\Form*
classes as these are what introspect theZend\Form\ElementInterface
objects and render the required HTML.For example:
Then just replace the default form element helper by registering a invokable with the same name.
You can use FormRow view helper, which will render non-valid elements with CSS class ("input-error" by default).
Usage is very simple, in your template:
or if you want custom class: