When you add filters and validators in zend framework 2, you may notice a validator being triggered that you have not actually added. In ZF2 validator, you might be trying to fix that with StringLength. But even min is specified as ‘0’, it still shows an error like:
Value is required and can't be empty
This is because of a required element of the array passed to filter and its value is true by default. You have to explicitly specify it as false to make the minimum length of string optional. Find below a complete the complete for an element with maximum length as specified and minimum length as optional.
$inputFilter - > add($factory - > createInput(array( 'name' => 'address2', 'required' => false, 'filters' => array( array('name' => 'StripTags'), array('name' => 'StringTrim'), ), 'validators' => array( array( 'name' => 'StringLength', 'options' => array( 'encoding' => 'UTF-8', 'max' => 128, 'messages' => array( StringLength::TOO_LONG => 'Address too long!', ) ) ) ), )));
As you can see ‘required’ => false must be specified to make the element value optional.
TESTED ON ZEND FRAMEWORK 2.3