0

我有一个登录表单和一个注册表单。两者都附加到同一个表用户。我如何在模型类中分别验证这两种形式。我尝试了两种不同的功能,代码如下

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))));
    }
}

我的疑问是控制器如何知道哪个功能用于登录或注册。我如何需要在控制器中编写代码来调用它????

4

2 回答 2

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

    }

}
于 2013-01-22T15:44:05.533 回答
1

我想这可能有效:-

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))));
}
于 2013-01-22T11:53:47.883 回答