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.

phalcon 2.0 custom validator problem

My validator crashed application. I found some answers but they don't seem to work to me. Not sure how to use custom validators under 2.0 below is how my validator looks like and what code adds it. Class is loaded

<?php
                           $validation->add($fieldName, new PhalconAppLibrary\Validation\Validator\UserNameValidator(array(
                               'message' => $this->t->_($fieldName.'-invalid')
                           )));
?>
<?
    namespace PhalconAppLibrary\Validation\Validator;

    use Phalcon\Validation\Validator,
    Phalcon\Validation\ValidatorInterface,
    Phalcon\Validation\Message;

class UserNameValidator extends Validator implements ValidatorInterface
{


    public function validate($validator, $attribute)
    {
    return true;
    }

}

?>

Doing something like this (and similar things)

>?
public function validate(\Phalcon\Mvc\ModelInterface $validator, $attribute)
?>

doesn't help.

What wrong did i do? I start thinking people don't use custom validators that often :)

What do you mean by "crashed my application"?



3.8k
Accepted
answer

try this

<?
    namespace PhalconAppLibrary\Validation\Validator;

    use Phalcon\Validation;
    use Phalcon\Validation\Validator,
    Phalcon\Validation\ValidatorInterface,
    Phalcon\Validation\Message;

class UserNameValidator extends Validator implements ValidatorInterface
{
    public function validate(Validation $validator, $attribute)
    {
        return true;
    }

}
?>


5.4k

Thank you ucando! I did something like this (Validation $validator) but just didn't do "use Phalcon\Validation;"

Well, Andres, i think i misused the word "crash" because it is used often with window application crashing not web. My app stopped working.