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

标签 php cakephp

在我的项目中,我有两个表单(登录和注册),从电子邮件和密码登录,注册表有用户名、电子邮件和密码,我按登录按钮,然后验证用户名所需的消息在表单中处于事件状态

我的问题是如何在单一 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/

相关文章:

php - CONCAT日期问题

php - 如何获取 whatsapp API (PHP) 的密码?

php - 如何将多个复选框值存储到一个数据库字段中

mysql - 如何比较条件中的两个字段/列?

cakephp - Cake php 和在布局中使用 auth

php - 上传前在 html 输入中显示所选文件

php - Mysql 'where clause' 有长表?

php - 对字符串数组进行排序,就像中间字符串数字位于字符串末尾一样

php - cakephp:使用一种形式保存到多个模型

mysql - CakePHP - 驻留在不同数据库上且具有不同查询语言的表之间的 $hasAndBelongsToMany 和 $hasMany 关系?