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.

Cannot get Model Validation working for email field

In Controller i have:

$email = $this->request->getPost('email');

            $obj = new Users();
            $valid = $obj->validation($email);

if(!$valid)
        {
            $this->flashSession->error("invalid email!");
        }
         else
         {
            $this->flashSession->success("good email!");
         }

In Model:

public function validation($email) { $this->validate( new Email( array( 'field' => 'email', 'required' => true, ) ) );

    if ($this->validationHasFailed() == true) {
        return false;
    }
}

Validation always returns error message. Please advise, what i am lacking!

$obj->validation shoud not be called directly, it's automatically called when you call "save" on that object.



6.8k
edited Apr '15

This is Sign In form, so I will not save any data here. I just need to validate the email address and have no clue how to do it correctly using "validation" method.

$obj->validation shoud not be called directly, it's automatically called when you call "save" on that object.



6.8k
edited Apr '15

Yes, you are right, Model Validation and Simple validation are different things and should be treated differently.