cakephp模型类中的多个验证函数

时间:2013-01-22 11:33:50

标签: php cakephp

我有登录表格和登记表格。并且两者都附加到同一个表用户。如何在Model类中单独验证两个表单。我尝试了两种不同的功能,代码在下面

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

我怀疑控制器是如何知道登录或注册的功能。我如何在控制器中编写代码来调用这个????

2 个答案:

答案 0 :(得分: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))));
}

答案 1 :(得分: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

    }

}