I made typical form with ZF2 form and wanted to add validation using ZF2 InputFilter. It was success but the colour of error message is black which is looks strange. I tried to change the colour by using method I've searched like this:
array(
'name' =>'NotEmpty',
'options' => array(
'messages' => array(
NotEmpty::IS_EMPTY => '<div style="color:red;">Please enter User Name!</div>'
),
),
),
But, instead of message's colour changed to red, it showed the tag with style, in other word, just plain HTML. What is the proper way to achieve my need?
Easiest way would be to modify the view helper ;)
Inside your module.config.php
'view_helpers' => [
'factories' => [
'formelementerrors' => function($vhm) {
$fee = new \Zend\Form\View\Helper\FormElementErrors();
$fee->setAttributes([
'class' => 'your error classes'
]);
return $fee;
}
]
]
The alternate approach when rendering errors using $this->formElementErrors()
would be to add the error classes inside the ViewHelper directly
$this->formElementErrors($element, ['class' => 'my error classes']);