Php 保存前保存验证数据

Php 保存前保存验证数据,php,cakephp-2.1,Php,Cakephp 2.1,我用cakePhp 2.x创建的网站有一个问题,当我试图注册一个帐户时,我的表单检查我所有字段的规则,然后在保存密码之前加密,但在检查密码之前加密密码(匹配密码)使用确认密码,然后返回错误,两个密码不相等,因为密码是包含40个字符的密码 这是我的模型代码,我如何解决这个问题 <?php //questo modello interessa lòa tabella User class User extends AppModel{ public $name =

我用cakePhp 2.x创建的网站有一个问题,当我试图注册一个帐户时,我的表单检查我所有字段的规则,然后在保存密码之前加密,但在检查密码之前加密密码(匹配密码)使用确认密码,然后返回错误,两个密码不相等,因为密码是包含40个字符的密码

这是我的模型代码,我如何解决这个问题

<?php
    //questo modello interessa lòa tabella User
    class User extends AppModel{
        public $name = 'User'; //non utilizzata nel sito è il nome del modello alla fine per migliorare la compatibilità

        public $validate = array(

            'password' => array(
                'non_vuoto' => array(
                    'rule'=> 'notEmpty',//non è vuoto metodo che eredito da appmodel
                    'message'=> 'La password non può essere vuota'  
                ),
                'min_lunghezza' => array(
                    'rule' => array('minLength',5),
                    'message' => 'La password deve contenere almeno 5 caratteri'
                ),
                'max_lunghezza' => array(
                    'rule' => array('maxLength',15),
                    'message' => 'La password deve contenere al massimo 15 caratteri'
                ),
                'password_uguale' => array(
                    'rule' => 'matchPasswords',
                    'message' => 'Not equal password'
                )
            ),
            'password_confirm' => array(
                'non_vuoto' => array(
                    'rule'=> 'notEmpty',//non è vuoto metodo che eredito da appmodel
                    'message'=> 'La password non può essere vuota'  
                )           
            )
        );


        public function matchPasswords($data){

            if ($data['password']==$this->data['User']['password_confirm']){
                return true;
            }

            $this->invalidate('password_confirm','Le due password non coincidono');
            return false;
        }


        public function beforeSave(){
            //crypt
            if (isset($this->data['User']['password'])){
                $this->data['User']['password']=AuthComponent::password($this->data['User']['password']);
            }
            return true;
        }

    }
?>

我遇到了类似的问题——我不确定这是否正是您所问的问题——但我需要在应用beforeSave()规则之前验证我的模型

我发现CakePHP的页面很有用。基本上,您可以设置数据

$this->ModelName->set($this->request->data);
然后可以检查模型的validates()方法

if ($this->ModelName->validates()) { // ...

然后,您可以决定是保存模型还是向用户显示错误,例如,在matchPasswords函数中使用$this->Session->setFlash()。

:它不应该是这样的吗?
$this->data['user']['password']==$this->data['user']['password\u confirm']
?不,因为使用“$data['password']”我认为密码在理论上没有加密。我尝试使用$this->data['User']['password']==$this->data['User']['password\u confirm']但效果不一样