CakePHP Validation nonBlank fails Field is Empty or !Empty
CakePHP Validation nonBlank fails Whether the Field is Empty or !Empty
文档说 'notBlank'
是您要确保它们不为空的字段的验证规则,如 !empty($somevalue)
中所示,但是当我将该字段留空时 ('')或者当我在字段 ('s0meCraZyPasSworD')
中输入一个值时它仍然显示错误消息?
谁能看出我做错了什么?其余的验证工作类似于最小长度,但我将它们注释掉以更好地了解为什么 'notBlank'
似乎不起作用...
CONTROLLER:
// Set of validation rules to be run
$validateRules = [
'fieldList' => [
'currentpassword',
'newpassword',
'confirmpassword'
]
];
if ($this->Admin->validates( $validateRules )) {
...
}
MODEL:
class Admin extends AppModel
{
public $name = 'Admin';
public $validate = [
'currentpassword' => [
'notBlank' => [
'rule' => 'notBlank',
'message' => 'Current password is required.'
]
],
...
您标记的 CakePHP 2.4 - notBlank
是 added in 2.7 so you have to use notEmpty or set allowEmpty to false...
The data sent to the model’s save() method must contain data for the
login field. If it doesn’t, validation will fail. The default value
for this key is boolean false.
required => true
does not mean the same as the validation rule
notBlank(). required => true
indicates that the array key must be
present - it does not mean it must have a value
来自官网:
http://book.cakephp.org/2.0/en/models/data-validation.html#required
因此您需要 allowEmpty 之类的其他规则来验证此字段,而不是 notBlank
规则。
文档说 'notBlank'
是您要确保它们不为空的字段的验证规则,如 !empty($somevalue)
中所示,但是当我将该字段留空时 ('')或者当我在字段 ('s0meCraZyPasSworD')
中输入一个值时它仍然显示错误消息?
谁能看出我做错了什么?其余的验证工作类似于最小长度,但我将它们注释掉以更好地了解为什么 'notBlank'
似乎不起作用...
CONTROLLER:
// Set of validation rules to be run
$validateRules = [
'fieldList' => [
'currentpassword',
'newpassword',
'confirmpassword'
]
];
if ($this->Admin->validates( $validateRules )) {
...
}
MODEL:
class Admin extends AppModel
{
public $name = 'Admin';
public $validate = [
'currentpassword' => [
'notBlank' => [
'rule' => 'notBlank',
'message' => 'Current password is required.'
]
],
...
您标记的 CakePHP 2.4 - notBlank
是 added in 2.7 so you have to use notEmpty or set allowEmpty to false...
The data sent to the model’s save() method must contain data for the login field. If it doesn’t, validation will fail. The default value for this key is boolean false.
required => true
does not mean the same as the validation rule notBlank().required => true
indicates that the array key must be present - it does not mean it must have a value
来自官网: http://book.cakephp.org/2.0/en/models/data-validation.html#required
因此您需要 allowEmpty 之类的其他规则来验证此字段,而不是 notBlank
规则。