25 февраля 2017 г.

cakephp bootstrap ui validation error messages not showing

I needed my CakePHP 3 application to automatically show the validation errors next to the coresponding FriendsOfCake/bootstrap-ui inputs. Here's what was the key point: you have to pass the corresponding model table instance, that contains those errors, into the $this->Form->create() call.

For example:

// in your controller
$user = $this->Users->newEntity();
if ($this->request->is('post')) {
    $user->set(array(
        // ... set you data to save
    ));
    $this->Users->save($user);
    debug($user->errors); // to confirm
}
$this->set('user', $user);

// in your .ctp file
$this->Form->create($user);

source

Комментариев нет: