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

Model Validators doesn't work

Hi, The problem is when try to validate it says that only email and username fields are required but it comes from auto PresenceOf model validation. So when I try to add for password Regex and/or PresenceOf it just doesn't work.

use Phalcon\Mvc\Model;
use Phalcon\Mvc\Model\Validator\Email as EmailValidator;
use Phalcon\Mvc\Model\Validator\Regex as RegexValidator;
use Phalcon\Mvc\Model\Validator\Uniqueness as UniquenessValidator;
use Phalcon\Mvc\Model\Validator\PresenceOf;

class Users extends Model
{

    public $id;
    public $email;
    public $password;
    public $username;
    public $type;
    public $token;

    public function validation()
    {

        $this->validate(new PresenceOf(
                array(
            "field" => "password",
            "message" => "Password is required !"
                )
        ));

        $this->validate(new RegexValidator(
                array(
            'field' => 'password',
            'pattern' => '/^\S*(?=\S{8,})(?=\S*[a-z])(?=\S*[A-Z])(?=\S*[\d])\S*$/',
            'message' => 'The password must be at least 8 characters long and must be a combination of upper and lower case letters and numbers.'
                )
        ));

        $this->validate(new EmailValidator(
                array(
            "field" => "email",
            "message" => "Email address is not correct"
                )
        ));

        $this->validate(new UniquenessValidator(array(
            'field' => 'email',
            'message' => 'You cant use thi e-mail, please try with another one!'
        )));

        return $this->validationHasFailed() != true;
    }

    public function getId()
    {
        return $this->id;
    }

    public function getEmail()
    {
        return $this->email;
    }

    public function getPassword()
    {
        return $this->password;
    }

    public function getUsername()
    {
        return $this->username;
    }

    public function getType()
    {
        return $this->type;
    }

    public function setId($id)
    {
        $this->id = $id;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }

    public function setPassword($password)
    {

        $this->password = $this->getDi()->getSecurity()->hash($password);
    }

    public function setUsername($username)
    {
        $this->username = $username;
    }

    public function setType($type)
    {
        $this->type = $type;
    }

    public function getToken()
    {
        return $this->token;
    }

    public function setToken($token)
    {
        $this->token = $this->getDi()->getSecurity()->hash($token);
    }

}


25.0k
edited Sep '14

Controller method:

        if (!empty($this->isAuth())) {
            $this->redirect(IndexEnom::HOME_URL);
        }

        $form = new SignUpForm();
        if ($this->request->isPost()) {

            $users = new Users();
            if ($users->save($this->request->getPost(), $form)) {
                $this->saveUserData($this->request->getPost('email'), $this->request->getPost('password'));
                $this->authAction();
            }

            foreach ($users->getMessages() as $message) {
                $this->flashSession->error($message->getMessage());
            }
        }

        $this->view->pick("index/signup");
        $this->view->form = $form;

FORM:

initialize

 $this->add(new Text("email"));

        $this->add(new Password("password"));

        $this->add(new Text("username"));

        $this->add(new Password("passwordAgain"));

        $this->add(new Hidden("token"));

        $this->add(new Select("type", array(
            '1' => 'normal',
            '2' => 'agent'
        )));

        $validation = new \Phalcon\Validation();

        $validation->add('passwordAgain', new Confirmation(array(
            'message' => ucfirst('passwords') . '\'s doesn\'t match to ',
            'with' => 'password'
        )));

        $messages = $validation->validate($this->request->getPost());
        if (count($messages)) {
            foreach ($messages as $message) {
                $this->flashSession->error($message);
            }
        }


33.8k

I think the problem is public function validation(). Inside it, you don't add validators, instead you validate. You had to add validators first, and then validate.



25.0k
edited Sep '14

But here is the example in the documentation:

<?php

use Phalcon\Mvc\Model\Validator\InclusionIn,
    Phalcon\Mvc\Model\Validator\Uniqueness;

class Robots extends \Phalcon\Mvc\Model
{

    public function validation()
    {

        $this->validate(new InclusionIn(
            array(
                "field"  => "type",
                "domain" => array("Mechanical", "Virtual")
            )
        ));

        $this->validate(new Uniqueness(
            array(
                "field"   => "name",
                "message" => "The robot name must be unique"
            )
        ));

        return $this->validationHasFailed() != true;
    }

}


25.0k
edited Sep '14

My question is how to validate field password. I think this happen because of the form ..



25.0k
Accepted
answer
edited Sep '14

I solve this like create my own validator for password and use it in model.