A free and open-source book on ZF3 for beginners

9.3. Validator Behaviour in Case of Invalid or Unacceptable Data

If you pass a validator some data that doesn't pass the check, the validator internally creates the list of error messages that can be retrieved with the getMessages() method. For example, look below for possible validation errors that the EmailAdrress returns if you pass it the "abc@ewr" value (the back-slash ('\') character indicates line breaks where code doesn't fit book page):

array(3) { 
  ["emailAddressInvalidHostname"] => 
    string(51) "'ewr' is not a valid hostname for the email address" 
  ["hostnameInvalidHostname"] => 
    string(66) "The input does not match the expected structure for a DNS hostname" 
  ["hostnameLocalNameNotAllowed"] => 
    string(84) "The input appears to be a local network name but local network names are not allowed" 

Validator's getMessages() method will return an array of messages that explain why the validation failed. The array keys are validation failure message identifiers, and the array values are the corresponding human-readable message strings.

If isValid() method was never called or if the most recent isValid() call returned true, then the getMessages() method returns an empty array. Also, when you call isValid() several times, the previous validation messages are cleared, so you see only validation errors from the last call.

Some validators may work with input data in certain format only (for example, a validator may require that the input data be a string, but not an array). If you pass it data in unacceptable format, the validator may throw an Zend\Validator\Exception\RuntimeException exception or raise a PHP warning.

It is recommended to check certain validator's documentation to be aware of its actual behaviour in case of inacceptable data.