Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Custom Validator


I would like to create a custom validator. In addition to the field value, I would like to add an other value to compare with. I don't know how to do it and it's not explained in the doc.

Could you help me ?


Do you perhaps mean something like the ConfirmationOf validator in the Phalcon Incubator Repo?

If you want to compare it to a value defined serverside i think you can get it to work with small changes in the code


Thank you for your answer but your example is for Model/Validatior. I would like an example for Validation/Validator.

edited Mar '15

Could you tell me why this code doesn't work. It doesn't raise exception, It just stops and does nothing.

That looks good to me. I just setup and tested it in a simple form and it looks ok to me

// Form is a simple <input type=text name=test> form
if($this->request->isPost()) {
    $validation = new Phalcon\Validation();

    $validation->add('test', new CheckPassword(array(
        'value' => 'foo' // has of expected password

    $messages = $validation->validate($this->request->getPost());

    if(count($messages)) {
        // Validation failed here

Your validator will not raise an error itself, Phalon\Validation will take care of that when you call $validation->validate();


It's weird because when I launch a debug, the debug stops at $validation->add('current', new CheckPassword(array( 'value' => 'password', 'message' => 'message to show to user' )))

edited Mar '15

Problem solved.

The problem came from two points.


public function validate(Validation $validator, $field) // is not right. 
public function validate($validator, $field) // It must be this even if my IDE highlights $validator


// The class wasn't registered. I had to use $loader->registerNamespaces() and to include the path to directory of my custom validators directory.
    'Phalcon\Validation\Validator'  => '../app/validators/'


Can anyone tell me why markdown doesn't work for me ?


Can anyone tell me why markdown doesn't work for me ?

Empty line before "```php" will help


Thanks a lot !