密码确认验证CakePHP [英] Password Confirm Validation CakePHP

查看:89
本文介绍了密码确认验证CakePHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索了很多,尝试了书中的每一个伎俩,但我仍然无法得到我的CakePHP应用程序来执行简单的密码确认验证。我已尝试创建如下所示的自定义验证规则:

I have searched far and wide, tried every trick in the book, but I still can't get my CakePHP application to perform simple Password Confirm validation. I've tried creating a custom validation rule like this:

'passwordequal' => array('rule' => 'checkpasswords' , 'message' => 'Passwords Do Not Match')


$ b b

然后定义'checkpasswords'这样:

Then defined 'checkpasswords' like this:

public function checkpasswords(){

    if(strcmp($this->data['User']['new_password'],$this->data['User']['confirm_password']) == 0 )
    {
        return true;
    }
    return false;
}

'new_password'和'confirm_password'是密码输入字段。这没有工作。然后我试过一个在其中我散列'confirm_password'。这也没有工作。我有其他'规则',以及没有被验证,像'notempty',我相信是标准的CakePHP规则之一。可以任何人请帮助。我知道这个问题已经被问了几次,但没有一个解决方案为我工作。 CakePHP文档对帮助很大。

'new_password' and 'confirm_password' are the password input fields. This didn't work. Then I tried one in which I hashed the 'confirm_password'. That didn't work either. I have other 'rules' as well that are not being validated, like 'notempty', which I believe is one of the standard CakePHP rules. Can anybody please help. I know this question has been asked a few times but none of those solutions have worked for me. CakePHP documentation hasn't helped much either.

推荐答案

视图文件中的两个字段

echo $this->Form->input('password');
echo $this->Form->input('repass');

模型文件

<?php
class Post extends AppModel {
    public $validate = array(
        'repass' => array(
            'equaltofield' => array(
            'rule' => array('equaltofield','password'),
            'message' => 'Require the same value to password.',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            'on' => 'create', // Limit validation to 'create' or 'update' operations
            )
        )
    );

function equaltofield($check,$otherfield)
    {
        //get name of field
        $fname = '';
        foreach ($check as $key => $value){
            $fname = $key;
            break;
        }
        return $this->data[$this->name][$otherfield] === $this->data[$this->name][$fname];
    } 
}?>

这篇关于密码确认验证CakePHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆