Just to throw my two cents, a custom message can also be customized via configuration. I often use this when using a factory approach like this:
'name' => array( ... 'validators' => array( new \Zend\Validator\Callback( array( 'messages' => array(\Zend\Validator\Callback::INVALID_VALUE => '%value% can only be Foo'), 'callback' => function($value){ return $value == 'Foo'; })) ) ),
This creates a message like "The bar can only be Foo."
Look carefully at the \Zend\Validator\Callback::INVALID_VALUE
key, this is the constant defined in \ Zend \ Validator \ Callback:
const INVALID_VALUE = 'callbackValue';
which is used in this class to set messages used by the validator:
protected $messageTemplates = array( self::INVALID_VALUE => "The input is not valid", self::INVALID_CALLBACK => "An exception has been raised within the callback", );
This means that you can safely use \Zend\Validator\Callback::INVALID_VALUE => 'Custom message'
I'm not sure if this violates the coding principle, someone please correct me if this happens.
source share