1

I'm using CakePHP 4.0, and creating a user registration page. I've set up some validation rules, which are returning errors when expected, but I can't figure out how to get them to show against the appropriate fields in the form. In v3.0 this seemed to happen automatically.

My form (in register.php) is:

echo $this->Flash->render();
echo $this->Form->create();
echo $this->Form->control('name');
echo $this->Form->control('email');
echo $this->Form->control('password', array('type' => 'password'));
echo $this->Form->control('confirm_password', array('type' => 'password'));

In UsersController.php I have the action register:

    public function register() {

        $user = $this->Users->newEmptyEntity();

        if($this->request->is('post')) {

            $user = $this->Users->patchEntity($user, $this->request->getData());
            if($user->getErrors()) {
                $this->Flash->error(__('Unable to register you.  Please make sure you have completed all fields correctly.'));
            }else {
                $this->Users->save($user);
                $this->Flash->success(__('Success'));
                return redirect($this->get_home());
           }

       }

       $this->set('user', $user);

   }

The validation rule in UsersTable is:

public function validationDefault(Validator $validator): Validator {

    $validator->requirePresence([
                'name' => [
                        'mode' => 'create',
                        'message' => 'Please enter your name'
                ],
                'email' => [
                        'mode' => true,
                        'message' => 'Please enter your email address'
                ]
            ])
            ->allowEmptyString('name', 'Name cannot be empty', false);
}

If I submit the form without entering anything for the name or email address, getErrors() picks up the invalid fields and creates an array, which I can see via debug contains:

'name' => [
        '_empty' => 'Name cannot be empty'
    ]

So it has realised that name field doesn't validate, but it doesn't show it against the field in the form (or anywhere) as it did in version 3.0.

What else do I need to do?

Sharon
  • 3,471
  • 13
  • 60
  • 93
  • 2
    https://book.cakephp.org/4/en/views/helpers/form.html#displaying-and-checking-errors – Salines Apr 29 '20 at 13:17
  • Thank you! I hadn't thought to look at the form part of the book! – Sharon Apr 29 '20 at 13:19
  • 2
    try to create form like echo $this->Form->create($user); – Salines Apr 29 '20 at 13:24
  • @Sharon did you get this to "just work" in Cake 4? I also recall that in Cake 3 it worked out of the box. In Cake 4 I've used the same code you have but I had to add extra code under each field as per https://book.cakephp.org/4/en/views/helpers/form.html#displaying-and-checking-errors to get any of the error messages to display inline on my form. – Andy Sep 08 '21 at 14:24
  • @Andy yes, see the answer below – Sharon Sep 10 '21 at 15:50
  • @Sharon I was using the code as in the given answer below. It didn't seem to work without putting code underneath each form field (e.g. `if ($this->Form->isFieldError('first_name')): echo $this->Form->error('first_name'); endif;`). CakePHP version is 4.2.8 in this case, which is presumably slightly later than what you were using. However, it still seems strange to need this code under each field as it did seem to work out of the box without it historically. – Andy Sep 12 '21 at 12:37

1 Answers1

1

Thanks to Salines, I used

echo $this->Form->create($user);

instead of

echo $this->Form->create();

and errors immediately showed up.

Sharon
  • 3,471
  • 13
  • 60
  • 93