phpzend-framework2zend-formzend-form-elementzend-validate

Set NotEmpty validator error message


I am trying to set a custom error message with ZF2 form and the NotEmpty validaor.

In my form I have the following radio element.

$this->add(array(
    'name' => 'paymentMethod',
    'type' => 'Radio',
    'options' => array(
        'label' => _('Payment Methods:'),
        'value_options' => $paymentMethods,
        'disable_inarray_validator' => TRUE,
    ),
));

and in my input filter I have

$inputFilter->add(array(
    'name' => 'paymentMethod',
    'required' => TRUE,
    'filters' => array(
        array('name' => 'Int'),
    ),
    'validators' => array(
        array(
            'name' => 'NotEmpty',
            'break_chain_on_failure' => true,
            'options' => array(
                'messages' => array(
                    NotEmpty::IS_EMPTY => _("You must select the payment method"),
                ),
            ),
        ),
        array(
            'name' => 'InArray',
            'options' => array(
                'haystacK' => $this->getPaymentMethods(),
                'messages' => array(
                    InArray::NOT_IN_ARRAY => _("This payment method does not exist"),
                ),
            ),
        ),
    ),
));

As can be seen in my input filter I have set a custom message for my NotEmpty validator. The problem I am having is that the form outputs the default error message 'Value is required and can't be empty' and not my custom one.

I assume that this has something to do with the 'required' => TRUE automatically setting a NotEmpty validator but I don't know how to disable this.

I have other text elements with this validator and they display the custom error message fine, this radio element just does not. I also have a multicheckbox element that has the same problem.

Does anyone know as to why this is happening?

Many thanks in advance.

EDIT

I am now having the same problem with another element, in this case its DoctrineModule\Form\Element\ObjectMultiCheckbox.

The element is defined

$this->add(array(
    'name' => 'categories',
    'type' => 'Admin\DoctrineModule\Form\Element\ObjectMultiCheckbox',
    'options' => array(
        'label' => _('Categories:'),
        'label_attributes' => array('class' => 'required'),
        'object_manager' => $this->getEntityManager(),
        'target_class' => 'Application\Entity\Categories',
        'property' => 'category',
        'is_method' => true,
        'find_method' => array(
            'name' => 'FindAll',
        ),
        'disable_inarray_validator' => TRUE,
    ),
));

and in my getInputFilterSpecification() method I have

...
'categories' => 
    array(
        'required' => TRUE,
        'allow_empty' => TRUE,
        'continue_if_empty' => TRUE,
        'filters' => array(
            array('name' => 'Int'),
        ),
        'validators' => array(
            array(
                'name' => 'NotEmpty',
                'break_chain_on_failure' => true,
                'options' => array(
                    'messages' => array(
                        NotEmpty::IS_EMPTY => _("You must select the items categories"),
                    ),
                ),
            ),
            array(
                'name' => 'Freedom\Zend\Validator\Doctrine\Db\DoctrineRecordExists',
                'options' => array(
                    'entityManager' => $this->getEntityManager(),
                    'entity' => 'Application\Entity\Categories',
                    'identifier' => 'catagoryId',
                    'messages' => array(
                        DoctrineRecordExists::ERROR_NO_RECORD_FOUND => _("This category does not exist"),
                    ),
                ),
            ),
        ),
    )
...

As you can see I have tried the 'allow_empty' and 'continue_if_empty' options but with no luck. I have also tried 'required' => FALSE, but the element simply passes validation.

The default message shows when NotEmpty validation fails.


Solution

  • Modify your input filter change the 'required' => TRUE, option to 'required' => false

    $inputFilter->add(array(
        'name' => 'paymentMethod',
        'required' => false,
        'filters' => array(
            array('name' => 'Int'),
        ),
        'validators' => array(
            array(
                'name' => 'NotEmpty',
                'break_chain_on_failure' => true,
                'options' => array(
                    'messages' => array(
                        NotEmpty::IS_EMPTY => _("You must select the payment method"),
                    ),
                ),
            ),
            array(
                'name' => 'InArray',
                'options' => array(
                    'haystacK' => $this->getPaymentMethods(),
                    'messages' => array(
                        InArray::NOT_IN_ARRAY => _("This payment method does not exist"),
                    ),
                ),
            ),
        ),
    ));