Validation Cakephp验证问题

Validation Cakephp验证问题,validation,cakephp,boolean,Validation,Cakephp,Boolean,嘿,伙计们,Cakephp验证有问题 我想知道为什么第二方验证会直接变为假 以下是我的关系模型: <?php class Relationship extends AppModel{ var $name='Relationship'; public $useTable = 'relationships_users'; public $primaryKey = 'id'; var $validate = array(

嘿,伙计们,Cakephp验证有问题

我想知道为什么第二方验证会直接变为假

以下是我的关系模型:

<?php
    class Relationship extends AppModel{
        var $name='Relationship';
        public $useTable = 'relationships_users';
        public $primaryKey = 'id';

        var $validate = array(
            'date' => array(
                'rule' => array('datevalidation', 'systemDate'),
                'message' => 'Current Date and System Date is mismatched'
            ),
            'partytwo'=>array(
                'partytwoExists'=>array(
                    'rule'=> 'userExists',
                    'message'=>'That username doesnt exist.'
                )
            )
        );

        function datevalidation( $field=array(), $compare_field=null ) {
            if ($field['date'] > $compare_field)
                return TRUE;
            else
                return FALSE;
        }

        function userExists($check) {
            $userExists= $this->find('count', array('conditions'=>$check));
            if($userExists == 1) {
                return TRUE;
            }else{
                return FALSE;
            }
        }
 ...

根据CakePHP书中的一节,一个自定义规则是这样写的

'rule' => array('datevalidation', 'systemDate')
意味着Cake将运行您的
datevalidation
方法,如下所示:

$valid = $Relationships->datevalidation(array(
    'date' => 'some user input value'
), 'systemDate');
以同样的方式,

'rule' => array('userExists')
使蛋糕流出来

$valid = $Relationships->userExists(array(
    'partytwo' => 'some user input value'
));
(模拟调用。实际调用使用Model.php的
dispatchMethod
at)

因此,您很可能需要重写
datevalidation
方法。此外,您的代码

$userExists= $this->find('count', array('conditions'=>$check));

$userExists
可以返回大于或等于0的数字。如果返回2或更多,则逻辑错误。考虑<代码>模型:这可能是您的案例总是将其验证为
false
的原因。

那么,您的
$check
参数中到底有什么?
$check
保留您要验证的字段
$this->data
保存您试图验证/保存的当前数据。记住这一点,更新您的代码。userExists方法中的目标是检查数据库中是否存在数据中的用户id。