Zend Framework 2: NotEmpty Validator Message

I'm having a form with an element username. There are two validators: NotEmpty and StringLength. The custom error messages for StringLength are working, but somehow it does not use the custom error message for the NotEmpty validator. In ZF1 the notEmpty validator was added automatically when making an element required which could be turned off. I can't find such an option in ZF2 and maybe my NotEmpty validator is not used because it was already added by the required flag!?

    $inputFilter->add($factory->createInput(array(
        'name'     => 'username',
        'required' => true,
        'filters'  => array(
            array(
                'name' => 'StringTrim'
            ),
        ),
        'validators' => array(
            array(
                'name' => 'NotEmpty',
                'options' => array(
                    'messages' => array(
                        NotEmpty::IS_EMPTY => 'Bitte geben Sie Ihren Benutzernamen ein.',
                    ),
                ),
            ),
            array(
                'name' => 'StringLength',
                'options' => array(
                    'min' => 3,
                    'max' => 45,
                    'messages' => array(
                        StringLength::TOO_SHORT => 'Der Benutzername muss mindestens 3 Zeichene lang sein.',
                        StringLength::TOO_LONG => 'Der Benutzername darf maximal 45 Zeichen lang sein.',
                    )
                ),
            ),
        ),
    )));

Answers


The ZF2 Form, by default, adds validators for the different form elements automatically when creating them. To prevent the defaults from being added and only have your validators present, you need to add to the form constructor:

$this->setUseInputFilterDefaults(false);

This will prevent the Form object from adding those default validators. Now when you add your own NotEmpty, or any other element for that matter, validator with a custom message, that message is used instead of the default one.


It was a bug in the version I was using. After updating to the latest revision on GitHub it works fine.


Need Your Help

Prevent Chrome from wrapping contents of joined <p> with a <span>

javascript html5 google-chrome contenteditable

I have observed an undesirable behaviour in Chrome that occurs when one joins two &lt;p&gt;'s by deleting the separation between them. Although the &lt;p&gt; tags are joined properly, Chrome wraps ...

What is better performance wise: Visibility converter VS changing the entire source

c# wpf ivalueconverter

In a WPF application, if I have an observable collection displayed in a grid/list view. And the user can request to filter what is visible by a predefined set of rules. What would be better perform...