3
votes

The docs say that 'notBlank' is a validation rule for fields that you want to make sure they are not empty, as in !empty($somevalue), but when I leave the field blank ('') or when I put a value in the field ('s0meCraZyPasSworD') it still display the error message?

Can anyone see what I'm doing wrong? The rest of the validations work like minlength, but I commented them out to get a better idea of why 'notBlank' doesn't appear to be working...

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.'
            ]
        ],
    ...
2

2 Answers

2
votes

You tagged CakePHP 2.4 - notBlank was added in 2.7 so you have to use notEmpty or set allowEmpty to false...

0
votes

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

From the official website: http://book.cakephp.org/2.0/en/models/data-validation.html#required

So you will need another rule like the allowEmpty to validate this field and not the notBlank rule.