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

How to use Phalcon validators as a simple function?

How to use Phalcon validators as a simple function? e.i. isValidEmail('[email protected]') // return a boolean

for your Model:

use Phalcon\Mvc\Model\Validator\Email;

    public function validation() {
    $this->validate(new Email(array(
        'field' => "email"
    )));       
    return $this->validationHasFailed() != true;
}
edited May '15

for your controller:

use Phalcon\Validation\Validator\Email;

 $validation = new Validation();
    $validation->add('user_email', new PresenceOf(array(
        'message' => _('Email es requerido')
    )))->add('user_email', new Email(array(
        'message' => _('El formato del email no es valido')
    )));

    $messages = $validation->validate($a_post);
    $s_error_validation = '';
    if (count($messages)) {
        foreach ($messages as $message) {
            $s_error_validation .=$message . '<br>';
        }
        throw new \Exception(_($s_error_validation), self::BTS_CODE_SUCCESS);
    }


22.1k

By "as a simple function" I mean without need to instantiat a Validation. I dont need message or similar stuff. Only true/false.

For now, I come up with (new \Egulias\EmailValidator\EmailValidator())->isValid($email))