Cake Validation: form elements not validating

Hi,

I’m trying to use Cake\Validation\Validator but I can’t get my form to validate properly. I’m requiring the presence of all form elements but only the username and email fields are validating. The array being displayed on the image below shows the key/value pairs being submitted.

Anyone any idea what the problem could be?

        return $validator
            ->requirePresence('username', 'This field is required')
            ->notEmptyString('username', 'Username is required')
            ->minLength('username', 3, 'Too short')
            ->maxLength('username', 60, 'Too long')
            ->requirePresence('password', 'This field is required')
            ->notEmptyString('password', 'Password is required')
            ->minLength('password', 8, 'Too short')
            ->maxLength('password', 60, 'Too long')
            ->requirePresence('email', 'This field is required')
            ->email('email', false, 'E-Mail must be valid')
            ->requirePresence('first_name', 'This field is required')
            ->notEmptyString('first_name', 'Input required')
            ->requirePresence('last_name', 'This field is required')
            ->notEmptyString('last_name', 'Input required');

Just had to remove the elements from the request and that triggered the errors:

There is a difference between “the field has not been submitted” and “the field has been submitted, but the value is empty”. Can you try it with Postman and post a screenshot?

Hi Odan,

Just using Postman seemed to sort this straight away! Where does the message “This field is required” get set? I’m setting it in the validator but when I change it the change isn’t reflected when I submit the request to postman.

You should also post a code snippet. The message is from the validation rule itself.

->requirePresence('password', 'This field is required')

Yes I should’ve posted some code. I changed the message on the last name field and there was no change in the JSON output. Here’s my function:

    /**
 * Create validator.
 *
 * @return Validator The validator
 */
private function createValidator(): Validator
{
    $validator = $this->validationFactory->createValidator();
    
    return $validator->requirePresence('username', 'This field is required')
                    ->notEmptyString('username', 'Username is required')
                    ->minLength('username', 3, 'Too short')
                    ->maxLength('username', 60, 'Too long')
                    ->requirePresence('password', 'This field is required')
                    ->notEmptyString('password', 'Password is required')
                    ->minLength('password', 8, 'Too short')
                    ->maxLength('password', 60, 'Too long')
                    ->requirePresence('email', 'This field is required')
                    ->email('email', false, 'E-Mail must be valid')
                    ->requirePresence('first_name', 'This field is required')
                    ->notEmptyString('first_name', 'Input required')
                    ->requirePresence('last_name', 'This field is required chummy')
                    ->notEmptyString('last_name', 'Input required');

   return $validator;         

  }
}

I figured this out, was very easy. Set true for the 2nd param then add the custom message as the 3rd param:

requirePresence('username', true, 'username must be entered')