【问题标题】:CakePHP: anyway to tell why Model->validates() returns false?CakePHP:无论如何要告诉为什么 Model->validates() 返回 false?
【发布时间】:2011-04-21 19:54:23
【问题描述】:

如果 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
        )

)

看起来应该通过验证。

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

更新:

这是模型。我正在使用 multivalidatable 行为。我尝试过使用默认和注册验证集:

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;
    }

}

【问题讨论】:

  • 你能展示你的模型吗?
  • 您设置了哪些验证要求?
  • @cdburgess @Amy 我已经更新了我的 Q 以包含我的模型。
  • 我会先取出 ValidationSets 看看它是否有效。如果是这样,那么它会将问题缩小到行为。
  • @cdburgess,很好,我会试试的!

标签: php cakephp cakephp-1.3


【解决方案1】:

我会从这样的事情开始:

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

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

【讨论】:

  • 是的 $this->User->validationErrors 这就是我要找的。​​span>
【解决方案2】:

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

'password' => array(
  'rule' => array('minLength', '8'),
  'message' => 'Mimimum 8 characters long'
),

另外,如果你开启了 AUTH,它可能会加密密码。

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

【讨论】:

  • 我的验证规则中确实设置了一条消息,但在这个特定的实例中,我没有使用表单来捕获数据。我正在使用保存在会话中的数据,(售后完成)然后告诉用户他们需要做的就是插入密码来创建帐户。所以没有表单来显示消息。这就是为什么我想知道是否有一种可编程性捕获消息的方法。 debug 中的输出未加密。
  • 实际上是因为其中一个密码被加密而另一个没有被加密所以他们没有“匹配”+1
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-03-07
  • 2013-09-18
  • 2014-11-17
  • 2014-03-11
  • 2019-07-12
  • 2012-08-24
相关资源
最近更新 更多