CakePhp 3.5 validation error mesages not showing on fields

Viewed 1596

so i started playing with a cakePhp app, trying to make it work, but i can't make the form helper output the validation error messages on the field with the error.I am using the FriendsOfCake/bootstrap-ui plugin to make the form helper output bootstrap forms. Here is my model:

 public function validationDefault(Validator $validator)
{

    $validator
        ->integer('id')
        ->allowEmpty('id', 'create');

    $validator
        ->notEmpty('name', 'Name should not be empty')
        ->scalar('name')
        ->requirePresence('name', 'create');
    return $validator; 

My controller method:

 $user = $this->Users->newEntity();
 if ($this->request->is('post')) {
     $user = $this->Users->patchEntity($user, $this->request->getData());
     if ($this->Users->save($user)) {
         $this->Flash->success(__('V-ati inregistrat cu success'));
            return $this->redirect($this->referer());
        }
     $this->Flash->error(__('The user could not be saved. Please, try again.'));
    }


 $this->viewBuilder()->setLayout('auth');
 $this->viewBuilder()->setTemplate('register');

 $this->set(compact('user'));
 $this->set('_serialize', ['user']);
 }

and my form:

<div class="container">
<div class="row">
    <div class="col-md-4 col-md-offset-4">

        <?=
            $this->Form->create('users',['url'=>['controller'=>'Users','action'=>'store']]),
            $this->Form->control('name'),
            $this->Form->control('email'),
            $this->Form->control('password'),
            $this->Form->submit('Trimite'),

            $this->Form->end()

        ?>

    </div>
</div>

1 Answers

You are passing an invalid context to the form helper. The $context argument of FormHelper::create() by default only supports false/null, an array, a result set, or an entity, and you want to pass the latter, ie the $user variable, which holds the entity that contains the possible validation errors.

$this->Form->create($user, [/* ... */])

See also

Related