The examples on this site are currently tested to work on Phalcon V3.4 and Phalcon Devtools V3.2 Some issues may arise when using later versions.

Please get in touch or post a comment below the post if you encounter a problem.

For many of the validators that could be added the argument can reasonably be made that this validation would be better done using client-side scripting. Proponents of client-side MVC frameworks such as Angular and React will reasonably claim that it's better to do a lot of this kind of validation using this mix of javascript based technologies.

It is worth considering, however, the evolutionary path that an application may take into the future. In the context of a development landscape which sees many applications evolving into REST(REpresentational State Transfer) based applications in order to take their place in a Service Oriented Architecture it may be useful to encode as many of the business rules into the Models as possible. This will ensure this Business Logic is still enforced even if the information going into and out of the application is emanating from separate applications.

In the following example we will look at adding a number of simple validators together in one Model class

While validation of dates might better be performed on the client side. There are a number of other types of validation which would be more difficult to do from the client side given that they require access to Server-Side data. One such example would be the Uniqueness validator which is typically used to check if a username chosen by a user has been already chosen by another user. This functionality allows users to select their own usernames while still ensuring they are unique.

As you may have many validation rules for each Model class the validation function may end up becoming substantial.

Before you can use a Validator you must add a use command for it at the top of the class. In this example we will use the DateValidator and the PresenceOf validator. The PresenceOf validator merely ensures the field is not empty.

use Phalcon\Validation as Validation;
use Phalcon\Validation\Validator\Date as DateValidator;
use Phalcon\Validation\Validator\PresenceOf as PresenceOfValidator;

Adding the PresenceOf Validator to the firstname and surname fields to ensure neither are left empty the Validation function now looks like this:

public function validation()
{
   $validator= new Validation();
   //create validators
   $dValidator = new DateValidator(["message" => "please use the format yyyy-mm-dd",
									"format" => "Y-m-d"]);
   $poValidator = new PresenceOfValidator();
   
   //add the validators to the various fields which need them
   $validator->add('dateofbirth', $dValidator); //adds a date validator to dateofbirth field
   $validator->add('firstname',$poValidator); //adds presence of validator to firstname field
   $validator->add('surname',$poValidator); //adds presence of validator to surname field
   
   //the validation function must return a validator object
   return $this->validate($validator);
}