CakePhp 3.5验证错误消息未显示在字段上

时间:2017-09-27 12:23:25

标签: php twitter-bootstrap cakephp

所以我开始玩cakePhp应用程序,尝试使其工作,但我无法使表单助手输出错误的字段上的验证错误消息。我正在使用FriendsOfCake/bootstrap-ui插件使表单助手输出bootstrap表单。 这是我的模特:

 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; 

我的控制器方法:

 $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']);
 }

和我的表格:

<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 个答案:

答案 0 :(得分:2)

您正在将无效的上下文传递给表单助手。默认情况下$context FormHelper::create()参数仅支持false / null,数组,结果集或实体,并且您希望通过后者,即$user变量,用于保存包含可能的验证错误的实体。

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

另见