Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
CakePHP:不管怎样,告诉我们为什么模型->;validates()返回false?_Php_Cakephp_Cakephp 1.3 - Fatal编程技术网

CakePHP:不管怎样,告诉我们为什么模型->;validates()返回false?

CakePHP:不管怎样,告诉我们为什么模型->;validates()返回false?,php,cakephp,cakephp-1.3,Php,Cakephp,Cakephp 1.3,如果Model->validates()返回false有没有简单的方法知道原因 我正试图保存一些数据,但无法保存。事实证明,它也不是在验证。如果我关闭验证,它会保存。虽然我需要验证它,但我不知道为什么它不验证 我使用了以下代码: if($this->User->create($user) && !$this->User->validates()) { debug($user); } 因此,如果它没有验证,它会告诉我数据是什么 它打印出这个: Ar

如果Model->validates()返回
false
有没有简单的方法知道原因

我正试图保存一些数据,但无法保存。事实证明,它也不是在验证。如果我关闭验证,它会保存。虽然我需要验证它,但我不知道为什么它不验证

我使用了以下代码:

if($this->User->create($user) && !$this->User->validates())
{
    debug($user);
}
因此,如果它没有验证,它会告诉我数据是什么

它打印出这个:

Array
(
    [User] => Array
        (
            [first_name] => Tim
            [last_name] => Zahn
            [phone] => 8833323235
            [email] => t@z.com
            [password] => ert
        )

    [Confirm] => Array
        (
            [email] => t@z.com
            [password] => ert
        )

)
看起来它应该通过验证

如果需要,我也可以发布我的模型源代码

更新: 这是模型。我正在使用这种行为。我已尝试使用默认验证集和寄存器验证集:

class User extends AppModel
{
    var $actsAs = array('Multivalidatable');

    var $hasOne = 'WishList';
    var $hasMany = array(
        'Address' => array(
            'conditions' => array('NOT' => array('Address.deleted' => '1')),
            'order' => array('Address.is_billing DESC')
        )
    );

    var $validate = array(
        'first_name' => array(
            'rule' => 'notEmpty'
        ),
        'last_name' => array(
            'rule' => 'notEmpty'
        ),
        'password' => array(
            'passRule1' => array(
                'rule' => 'notEmpty',
                'message' => 'Please enter a password'
            ),
            'passRule2' => array(
                'rule' => array('identicalFieldValues', 'password'),
                'message' => 'Passwords do not match'
            )
        ),
        'email' => array(
            'emailRule1' => array(
                'rule' => 'email',
                'message' => 'You must specify a valid email address'
            ),
            'emailRule3' => array(
                'rule' => array('identicalFieldValues', 'email'),
                'message' => 'Emails do not match'
            )
        )
    );

    var $validationSets = array(
        'register' => array(
            'first_name' => array(
                'rule' => 'notEmpty'
            ),
            'last_name' => array(
                'rule' => 'notEmpty'
            ),
            'password' => array(
                'passRule1' => array(
                    'rule' => 'notEmpty',
                    'message' => 'Please enter a password'
                ),
                'passRule2' => array(
                    'rule' => array('identicalFieldValues', 'password'),
                    'message' => 'Passwords do not match'
                )
            ),
            'email' => array(
                'emailRule1' => array(
                    'rule' => 'email',
                    'message' => 'You must specify a valid email address'
                ),
                'emailRule2' => array(
                    'rule' => 'isUnique',
                    'message' => 'That email address is already in our system'
                ),
                'emailRule3' => array(
                    'rule' => array('identicalFieldValues', 'email'),
                    'message' => 'Emails do not match'
                )
            )
        ),
        'billing' => array(
            'email' => array(
                'emailRule1' => array(
                    'rule' => 'email',
                    'message' => 'You must specify a valid email address'
                ),
                'emailRule3' => array(
                    'rule' => array('identicalFieldValues', 'email'),
                    'message' => 'Emails do not match'
                )
            )
        )
    );

    public function beforeValidate()
    {
        parent::beforeValidate();

        // if password is empty reset from hash to empty string
        if (isset($this->data['User']['password']) && $this->data['User']['password'] == Security::hash('', null, true)) {
                $this->data['User']['password'] = '';
        }
        return true;
    }

    function identicalFieldValues($field=array(), $compare_field=null)
    {
        foreach ($field as $key => $value)
        {
            $v1 = $value;
            $v2 = $this->data["Confirm"][$compare_field];
            if ($compare_field == 'password' && $v2 != '')
            {
                $v2 = Security::hash($v2, null, true);
            }
            if ($v1 !== $v2)
            {
                return false;
            } 
            else
            {
                continue;
            }
        }
        return true;
    }

}

您是否在验证中设置了消息,以便它可以告诉您失败的位置,例如:

'password' => array(
  'rule' => array('minLength', '8'),
  'message' => 'Mimimum 8 characters long'
),
此外,如果您启用了AUTH,则它可能正在加密密码


此外,在尝试创建/保存之前,您应该断言验证。

是否在验证中设置了消息,以便它可以告诉您失败的位置,例如:

'password' => array(
  'rule' => array('minLength', '8'),
  'message' => 'Mimimum 8 characters long'
),
此外,如果您启用了AUTH,则它可能正在加密密码


另外,您应该在尝试创建/保存之前断言validate。

我将从以下内容开始:

if($this->User->create($user) && !$this->User->validates())
{
    debug($user);
    debug($this->User->validationErrors);
}

这将准确地告诉您哪些字段未通过验证以及原因。

我将从以下内容开始:

if($this->User->create($user) && !$this->User->validates())
{
    debug($user);
    debug($this->User->validationErrors);
}

这应该可以准确地告诉您哪些字段没有通过验证以及为什么。

您可以展示您的模型吗?您设置了哪些验证要求?@cdburges@Amy我已经更新了我的Q,以包括我的模型。我将首先取出验证集,看看它是否有效。如果是这样的话,那么它将把问题缩小到行为上。@cdburges,很好的观点,我会试试看!你能展示你的模型吗?你设定了什么样的验证要求?@cdburges@Amy我已经更新了我的Q以包含我的模型。我会先拿出验证集,看看它是否有效。如果是这样的话,那么它将把问题缩小到行为上。@cdburges,很好的观点,我会试试看!我的验证规则中确实有一个消息集,但在这个特定实例中,我没有使用表单来捕获数据。我正在使用会话中保存的数据(售后服务完成),然后告诉用户他们需要做的就是插入密码来创建帐户。因此没有显示消息的表单。这就是为什么我想知道是否有一种方法可以通过编程捕获消息。
debug
中的输出未加密。实际上,这是因为其中一个密码已加密,而另一个密码未加密,因此它们不“匹配”+1我在验证规则中设置了一个消息集,但在这个特定实例中,我没有使用表单捕获数据。我正在使用会话中保存的数据(售后服务完成),然后告诉用户他们需要做的就是插入密码来创建帐户。因此没有显示消息的表单。这就是为什么我想知道是否有一种方法可以通过编程捕获消息。
debug
中的输出没有加密。实际上,这是因为其中一个密码被加密,而另一个密码没有加密,所以它们没有“匹配”+1是的
$this->User->validationErrors
这就是我要找的。是的
$this->User->validationErrors
这就是我要找的。