在我的项目中,我有两个表单(登录和注册),从电子邮件和密码登录,注册表有用户名、电子邮件和密码,我按登录按钮,然后验证用户名所需的消息在表单中处于事件状态
我的问题是如何在单一 View 和单一模型中验证 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',
),
);
最佳答案
您可以创建登录验证规则,但这确实违背了内置身份验证组件的目的。
更好地利用您的时间可以为注册表单的每个功能创建验证规则,也许还可以更改用户密码表单......
例如,在用户模型中创建一个函数来验证用户注册和用户更改密码:
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();
}
然后在 Controller 中相应地更改代码。
$this->User->validates()
在用户注册函数等中变为 $this->User->registerValidate()
希望这有帮助。
关于php - 如何在cakephp中验证一个模型中的两个表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32270040/