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

Hi,

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 ?

Thanks.

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



1.4k

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



1.4k
edited Mar '15

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

http://pastebin.com/ZsFgYAKR

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

<?php
// 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();



1.4k

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' )))



1.4k
edited Mar '15

Problem solved.

The problem came from two points.

1)

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

2)

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


1.4k

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



631
Accepted
answer

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

Empty line before "```php" will help



1.4k

Thanks a lot !