如果模型 - > 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;
}
}
答案 0 :(得分:3)
我会从这样的事情开始:
if($this->User->create($user) && !$this->User->validates())
{
debug($user);
debug($this->User->validationErrors);
}
这应该告诉您哪些字段或字段没有通过验证以及原因。
答案 1 :(得分:1)
您是否在验证中设置了消息,以便它可以告诉您失败的位置,例如:
'password' => array(
'rule' => array('minLength', '8'),
'message' => 'Mimimum 8 characters long'
),
此外,如果您启用了AUTH,则可能正在加密密码。
此外,您应该在尝试创建/保存之前断言验证。