Jake Jake - 1 month ago 16
PHP Question

Zend Framework 2 only show required custom validation error message

I have a required ISBN field that I would like to change the generic error messages for. I have set up the custom error messages and they work. The filters are part of the

getInputFilterSpecification()
function of the models
fieldSet
which validates and shows the custom error messages correctly:

use Zend\Form\Element;
use Zend\Form\Fieldset;
use Zend\Validator;
use Zend\InputFilter\InputFilterProviderInterface;
use Zend\Stdlib\Hydrator\ClassMethods as ClassMethodsHydrator;

class BookItemFieldset extends Fieldset implements InputFilterProviderInterface
{
public $Types;

public function __construct($itemName, $Types)
{

parent::__construct($itemName, $Types);

$this->Types = $Types;

// .. Other fields

$bookISBN = new Element\Number('bookISBN');
$bookISBN->setLabel('Book ISBN ')
->setAttribute('id', 'bookISBN');

// .. more fields

$this->add($bookISBN);
}

public function getInputFilterSpecification()
{
return array(

'bookISBN' => array(
'required' => true,
'validators' => array(
new Validator\NotEmpty(array(
'setMessage'=> 'ISBN is Required'
)
),
new Validator\Isbn(array(
'setMessage'=> 'ISBN is Invalid'
)),

)
),
//... more input filters

);

}
}


But when the field is left blank. Both "ISBN is Invalid" and "ISBN is required" messages appear.

Is there a way to only show the required error message if the field is left blank?

something like the following:

'bookISBN' => array(
'required' => array(
'required' => true,
'setMessage' => 'ISBN is required' // Only this shows if field is empty
),
'validators' => array(
new Validator\Isbn(array(
'setMessage'=> 'ISBN is Invalid' // only this shows if the input is invalid
)),

)
),


Thanks.

Answer

You need to break the chain by setting it to true, which by default is set to false by Zend. Can you please try and see if below code works.

public function getInputFilterSpecification()
    {
        return array(

            'bookISBN' => array(
                'required' => true,
              'validators' => array(
                  array(
                     'name' => 'not_empty',
                     'break_chain_on_failure' => true,
                     'options' => array(
                         'messages' => array(
                              \Zend\Validator\NotEmpty::IS_EMPTY => 'ISBN is required',
                         ),
                     ),
                  ),
                  new Validator\Isbn(array(
                      'setMessage'=> 'ISBN is Invalid'
                  )),

              )
            ),
           //... more input filters

        );

    }

or may be it is 'NotEmpty'. I can't test it out.

public function getInputFilterSpecification()
    {
        return array(

            'bookISBN' => array(
                'required' => true,
              'validators' => array(
                  array(
                     'name' => 'NotEmpty',
                     'break_chain_on_failure' => true,
                  ),
                  new Validator\Isbn(array(
                      'setMessage'=> 'ISBN is Invalid'
                  )),

              )
            ),
           //... more input filters

        );

    }
Comments