如何在cakephp中的一个模型中验证两个表单

时间:2015-08-28 11:28:45

标签: php cakephp

在我的项目中我有两个表格(登录和注册)登录电子邮件和密码和注册表格有用户名,电子邮件和密码我按登录按钮然后验证用户名所需的消息是活动的形式

我的问题是如何在单一视图和单一模型中验证2表单 我的验证文字就像,

public $validate=array(
   'email' => array(
   'rule'=>'email',
   'message'=>'The email field is not currect',
  ),
  'password' =>array(
   'rule'=>array('minLength','8'),
   'required' => true,
   'message'=>'minume 8 charecter long',
   'required'=>true,
  ),
  'user_name'=>array(
   'rule'=>'notEmpty',
   'required'=>true,
   'message'=>'user Name is require',
  ),

 );

1 个答案:

答案 0 :(得分:2)

您可以为登录创建验证规则,但它确实违背了内置Auth组件的目的。

更好地利用您的时间将为注册表单的每个功能创建验证规则,并且可能更改用户密码表单...

例如,在用户模型中创建一个函数来验证用户注册和用户更改其密码:

    public function registerValidate() {
        $validate1 = array(
            'username' => array(
                'mustNotEmpty'=>array(
                    'rule' => 'notEmpty',
                    'message'=> 'Please enter a username',
                    'last'=>true),
                'mustUnique'=>array(
                    'rule' =>'isUnique',
                    'message' =>'That username is already taken',)
            ),
            'email'=> array(
                'mustNotEmpty'=>array(
                    'rule' => 'notEmpty',
                    'message'=> 'Please enter email',
                    'last'=>true),
                'mustBeEmail'=> array(
                    'rule' => array('email'),
                    'message' => 'Please enter valid email',
                    'last'=>true),
                'mustUnique'=>array(
                    'rule' =>'isUnique',
                    'message' =>'This email is already registered',)
            ),
            'password'=>array(
                'mustNotEmpty'=>array(
                    'rule' => 'notEmpty',
                    'message'=> 'Please enter password',
                    'on' => 'create',
                    'last'=>true),
                'mustBeLonger'=>array(
                    'rule' => array('minLength', 6),
                    'message'=> 'Password must be greater than 5 characters',
                    'on' => 'create',
                    'last'=>true),
                'mustMatch'=>array(
                    'rule' => array('verifies'),
                    'message' => 'Both passwords must match'),
            )
        );

        $this->validate = $validate1;
        return $this->validates();

    }

    public function changePassword() {
        $validate1 = array(
            'password'=>array(
                'mustNotEmpty'=>array(
                    'rule' => 'notEmpty',
                    'message'=> 'Please enter your password',
                    'on' => 'create',
                    'last'=>true),
                'mustMatch' => array(
                    'rule' => array('checkpassword'),
                    'message'=> 'Old password is incorrect',
                    'on' => 'update',
                    'last'=>true),
            ),  
            'newpassword'=>array(
                'mustNotEmpty'=>array(
                    'rule' => 'notEmpty',
                    'message'=> 'Please enter a new password',
                    'on' => 'update',
                    'last'=>true),
                'mustBeLonger'=>array(
                    'rule' => array('minLength', 6),
                    'message'=> 'Password must be greater than 5 characters',
                    'on' => 'update',
                    'last'=>true),
                'mustMatch'=>array(
                    'rule' => array('newverifies'),
                    'message' => 'Both passwords must match'),
            )
        );

        $this->validate = $validate1;
        return $this->validates();

    }

然后在控制器中相应地更改代码。

$this->User->validates()在您的用户注册功能等中变为$this->User->registerValidate()

希望这有帮助。