0
votes

I'm facing a problem with the validation message upon the notBlank rule. (I use notBlank as notEmpty has been deprecated).

I tried to change the default validation message with notBlank rule which is 'This field cannot be left empty' to a custom one.

What I did works with all the other rules (including notEmpty) but does not work for notBlank and I don't understand why....

I tried in the both folowing ways :

$validator
            ->requirePresence('title', true, MSG_FORM_FIELD_REQUIRED)
            ->add("title", [
                "notBlank" => [
                    "rule" => "notBlank",
                    "message" => MSG_FORM_FIELD_REQUIRED
                ]
            ]);

OR

    $validator
        ->requirePresence('title', true, MSG_FORM_FIELD_REQUIRED)
        ->notBlank('title', MSG_FORM_FIELD_REQUIRED);

Am I missing something there ?

2
I made the same mistake in thinking notEmpty is deprecated. There used to be two notEmpty functions, only one is deprecated. You can still use $validator->notEmpty(...). notBlank is for use with the $validator->add method, like your first example (which does look like it should work...) - Greg Schmidt

2 Answers

-1
votes

Cakephp 3.2 I have same problem with requirePresence

$validator
        ->requirePresence('any_tx',['message'=>'Please enter a value']);
        return $validator;

Always returns "This field is required"

-1
votes

CakePHP 3+, just simply define notBlank and notEmpty together.

 $validator
    ->notEmpty('title', true, MSG_FORM_FIELD_REQUIRED)
    ->notBlank('title', MSG_FORM_FIELD_REQUIRED);

// Note: add notEmpty before notBlank method, plz refer https://github.com/cakephp/cakephp/issues/5856