Add class attribute to Form Errors

devanerd picture devanerd · Dec 7, 2012 · Viewed 8.3k times · Source

I´m developing an application using Zend Framework 2 and I use FormRow helper to render a label, the input and errors (if present) in a Form.

//within the view
echo $this->formRow($form->get('Name'));

When a user submits the form without filling the required input text field FormRow render´s it with the following error message:

<label>
    <span>Name: </span>
    <input class="input-error" type="text" value="" placeholder="Insert Name Here" name="Name">
</label>
<ul>
    <li>Value is required and can't be empty</li>
</ul>

How can I set a class for the li tag to style it afterwards?

I know that I can echo the formElementErrors with the desired class attribute via..

<?php echo $this->formElementErrors($form->get("Name"), array('class' => "valuerequired", 'message' => "errortestmessage")); ?>

..but FormRow will still render the error message without the class.

Just for reference I´m setting the entity this way:

public function getInputFilter()
    {
        if (!$this->inputFilter) {
            $inputFilter = new InputFilter();

            $factory = new InputFactory();

            $inputFilter->add($factory->createInput(array(
                'name'     => 'Name',
                'required' => true,
                'filters'  => array(
                    array('name' => 'StripTags'),
                    array('name' => 'StringTrim'),
                ),
                'validators' => array(
                    array(
                        'name'      => 'StringLength',
                        'options' => array(
                            'encoding' => 'UTF-8',
                            'min'      => 1,
                            'max'      => 100,
                        ),
                    ),
                ),
           )));

            $this->inputFilter = $inputFilter;
        }
        return $this->inputFilter;
    }

Answer

Sam picture Sam · Dec 7, 2012

See the code of formElementErrors

Basically you could do something like:

$this->formElementErrors($elem)
     ->setMessageOpenFormat('<ul%s><li class="some-class">')
     ->setMessageSeparatorString('</li><li class="some-class">');

But that is quite unhandy...

The better solution would be to extend the Zend\Form\View\Helper\FormElementErrors by your own class and then register the view-helper formElementErrors to your class. So basically you'd have something like this:

namespace Mymodule\Form\View\Helper;

use Zend\Form\View\Helper\FormElementErrors as OriginalFormElementErrors;

class FormElementErrors extends OriginalFormElementErrors  
{
    protected $messageCloseString     = '</li></ul>';
    protected $messageOpenFormat      = '<ul%s><li class="some-class">';
    protected $messageSeparatorString = '</li><li class="some-class">';
}

Last thing then would be to register the view helper with this new Class. For this you provide the following code inside your Modules Module.php

public function getViewHelperConfig()
{
    return array(
        'invokables' => array(
            'formelementerrors' => 'Mymodule\Form\View\Helper\FormElementErrors'
        ),
    );
}

displaimer: This code isn't tested, let me know if there are some errors, but i think this should work out quite well.