A free and open-source book on ZF3 for beginners


9.1. About Validators

A validator is designed to take some input data, check it for correctness, and return a boolean result telling whether the data is correct. If the data is incorrect, the validator generates the list of errors describing why the check didn't pass.

9.1.1. ValidatorInterface

In ZF3, a validator is a usual PHP class which implements the ValidatorInterface interface (it belongs to Zend\Validator namespace). The interface definition is presented below:

<?php
namespace Zend\Validator;

interface ValidatorInterface
{
  // Returns true if and only if $value meets the validation requirements.
  public function isValid($value);

  // Returns an array of messages that explain why 
  // the most recent isValid() call returned false. 
  public function getMessages();
}

As you can see, the ValidatorInterface has two methods: the isValid() method (line 7) and getMessages() method (line 11).

The first one, isValid() method, is intended to perform the check of the input value (the $value parameter). If the validation of the $value passes, the isValid() method returns boolean true. If the $value fails validation, then this method returns false.

A concrete validator class implementing the ValidatorInterface interface may have additional methods. For example, many validator classes have methods allowing to configure the validator (set validation options).


Top