无法显示 cakephp3 的错误消息 bootstrap-ui

Unable to display error message for cakephp3 bootstrap-ui

你做了什么

App\View 命名空间 App\View;

use Cake\View\View;
use BootstrapUI\View\UIViewTrait;

class AppView extends View
{
    use UIViewTrait;

    /**
     * Initialization hook method.
     */
    public function initialize()
    {
        //render the initializeUI method from the UIViewTrait
        $this->initializeUI();
        $this->layout('default');
    }
}

controller/create 函数

$student = $this->Students->newEntity($data['Students']);
if (!$this->Students->save($student)) {
    $isOkay = false;
    debug($student);
}

create.ctp

<?php echo $this->Form->create(null, ['align' => [
   'sm' => [
       'left' => 4,
       'middle' => 8,
       'right' => 12
   ],
   'md' => [
       'left' => 2,
       'middle' => 6,
       'right' => 4
   ]

], 'id' => 'myform']); ?>

<fieldset id="page-1">
   <legend>Student Profile</legend>

   <?php
       echo $this->Form->input('Students.name', [
           'label' => 'Full name'
       ]);
   ?>
</fieldset>

<button class="btn btn-primary" type="submit">Save</button>
<?php echo $this->Form->end(); ?>

StudentsTable.php

public function validationDefault(Validator $validator) {
  $validator
    - > integer('id') - > allowEmpty('id', 'create');

  $validator
    - > notEmpty('name', 'Username must input.');
}

预期行为

我预计我会在字段旁边出现错误块。

实际行为

页面中缺少错误消息,但我可以在控制器中找到它。

您需要将 $student 变量从控制器传递到视图 create.ctp。您还需要将 $student 变量传递到要显示错误的表单中。在你的情况下它将是:

$this->Form->create($student, ['align' => [...

注意:在这两种情况下$student变量应该是StudentsTable.php.

的实体