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.
inputFilteris part of the entity'sFieldSet- Jake