We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

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"?



6.7k
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;
    }

}
?>


9.8k

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.