0

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.

Garry
  • 1,455
  • 1
  • 15
  • 34

3 Answers3

0

I made some changes in your code but i did not test. Try and let me know if it worked. Good luck

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

));

dixromos98
  • 756
  • 5
  • 18
  • Thank you @dixromos98 for your answer, I have modified my code as per yours but am still getting the default message. – Garry Feb 15 '16 at 20:09
0

I have found the solution to my problem. i changed the 'required' => TRUE, option to 'allow_empty' => TRUE, and now my error message is showing correctly.

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

I assume not setting the required option to true stops the auto creation of a NotEmpty validator.

Garry
  • 1,455
  • 1
  • 15
  • 34
0

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"),
                ),
            ),
        ),
    ),
));
Malay M
  • 1,659
  • 1
  • 14
  • 22