Reputation: 1107
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 ????
Upvotes: 0
Views: 626
Reputation: 4313
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
}
}
Upvotes: 1
Reputation:
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))));
}
Upvotes: 1