0
votes

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.

1
Can you please provide the complete code. I believe the above code is not part of a inputFilter function. - Kamal Soni
@KamalSoni, I have added more code, the inputFilter is part of the entity's FieldSet - Jake

1 Answers

1
votes

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

        );

    }