0

I have a login form and a registration form . And both are attached to same table users. How do i validate both forms seperately in Model class. I have tried with two different functions, the code is below

class User extends AppModel
{
public function login()
  {
    public $private = array('username'=>array('required'=>array('rule'=>'notEmpty','message'=>'Username is reqired . So please fill the username')),'password'=>array('required'=>array('rule'=>'notEmpty','message'=>'Please enter your password.')));
    }
public function register()
    {
        public $private = array('firstname'=>array('required'=>array('rule'=>'notEmpty','message'=>'Enter your first name'),'lastname'=>array('required'=>array('rule'=>'notEmpty','message'=>'Enter your lastname name'),'username'=>array('required'=>array('rule'=>'notEmpty','message'=>'Username is reqired . So please fill the username')),'password'=>array('required'=>array('rule'=>'notEmpty','message'=>'Please enter your password.')),'role'=>array('valid'=>array('rule' => array('inList', array('admin', 'author')),'message' => 'Please enter a valid role','allowEmpty' => false))));
    }
}

My doubt is how wil the controller know which function is for login or register. How do i need to write the code in controller to call this ????

5
  • 1
    a) dont make it private b) usually you have one common public $validate array here. Try to read the docs about validation. Commented Jan 22, 2013 at 11:38
  • But with only one $validate how can i validate 2 forms ? Commented Jan 22, 2013 at 11:39
  • you don't validate the form, you validate the data. Cake doesn't care where the data comes from, providing it is in the correct format. Whatever data is provided to your model, cake will validate it Commented Jan 22, 2013 at 11:43
  • @Ross Thanks and if i dnt use 2 functions and write all validations for $validate will it work? Commented Jan 22, 2013 at 11:47
  • 1
    You need one public $validate array containing all of your fields and the associated rules. When you $this->Model->save($data) cake will validate it. This is basic cake principles and covered thoroughly in the documents. Commented Jan 22, 2013 at 11:57

2 Answers 2

1

I guess this may work :-

class User extends AppModel
{

    public $private = array('firstname'=>array('required'=>array('rule'=>'notEmpty','message'=>'Enter your first name'),'lastname'=>array('required'=>array('rule'=>'notEmpty','message'=>'Enter your lastname name'),'username'=>array('required'=>array('rule'=>'notEmpty','message'=>'Username is reqired . So please fill the username')),'password'=>array('required'=>array('rule'=>'notEmpty','message'=>'Please enter your password.')),'role'=>array('valid'=>array('rule' => array('inList', array('admin', 'author')),'message' => 'Please enter a valid role','allowEmpty' => false))));
}
Sign up to request clarification or add additional context in comments.

Comments

1
class User extends AppModel {

    public $validateLogin = array(
       // validate rules for the login form submission
    );

    public $validateRegister = array(
        // validate rules for the registration form submission
    );

}

class UsersController extends AppController {

    public function login() {

        $this->User->validate = $this->User->validateLogin;

        // handle login form submission

    }

    public function register() {

        $this->User->validate = $this->User->validateRegister;

        // handle register form submission

    }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.