验证消息未显示在 cakephp 3 最新版本中的字段下方

Validation message not showing below the field in cakephp 3 latest version

我正在尝试验证已应用 jquery 选择的多个 select 字段。

验证工作正常,但唯一的问题是验证消息未显示在输入字段下方。

这是我的文件。

profile_edit.ctp

<?php echo $this->Form->create($user,['action' => '', 'role'=>"form",'novalidate'=>true,'method'=>'post','id'=>'ProfileForm','templates'=>['label'=>false,'inputContainer'=>'{{content}}']]); ?>

<?php echo $this->Form->control('user_grades[].grade_id',['multiple','hiddenField'=>false, 'escape'=>false, 'type'=>'select', 'id'=>'sp_grade', 'options'=>App\Model\Table\GradesTable::getGrades('list'),'class'=>'form-control chosen-select']); ?>
<button type="submit" class="btn footer_btns float-left">Save</button>

<?php echo $this->Form->end(); ?>

MyAccountController.php

<?php
public function profileEdit(){

    $user = $this->Users->get($this->Auth->user('id'), ['contain'=>['UserGrades']]);

    if($this->request->is(['put','post'])){

        $data = $this->request->getData();

        if(isset($data['user_grades']) && !empty($data['user_grades'])) {
            $this->UserGrades->deleteAll(['user_id' => $this->Auth->user('id')]);
        }

        if(null == $this->request->getData('user_grades')){
            $this->request = $this->request->withData('user_grades.0.grade_id','');
        }

        $user = $this->Users->patchEntity($user, $this->request->getData(), [
            'validate' => 'editProfileSection',
            'associated' => [
                'UserGrades'  => ['validate'=> 'editProfileSection']
            ]
        ]);

        if(empty($user->getErrors())){
            if ($this->Users->save($user)) {
                $this->Flash->success(__('Succesfully updated <strong>'.$user->full_name .'</strong> Information||Success'));
                return $this->redirect(['action' => '']);
            }
        }
        $this->Flash->error(__('Please check your inputs and try again.||Action Failed!'));

    }
    $this->set(compact('user'));
}

UserGradesTable.php

   <?php 
   namespace App\Model\Table;

   use Cake\ORM\Table;
   use Cake\ORM\Query;
   use Cake\ORM\TableRegistry;
   use Cake\Event\Event;
   use Cake\ORM\RulesChecker;
   use Cake\Validation\Validator;   

   class UserGradesTable extends Table {

   public function initialize(array $config) {
       $this->addBehavior('Timestamp');
       $this->addBehavior('Trim');
   }

   public function validationEditProfileSection(Validator $validator) {
       $validator
           ->notEmpty('grade_id',"Please select at least one grade.");

       return $validator;
   }
}

我尝试获取错误消息并得到以下信息:

        Array
        (
            [user_grades] => Array
                (
                    [0] => Array
                        (
                            [grade_id] => Array
                                (
                                    [_empty] => Please select at least one grade.
                                )

                        )

                )

        )

但是这个错误没有显示在输入字段下面。任何帮助将不胜感激。

您没有为表单控件使用正确的命名方案,您不能使用 [],如果您希望表单助手魔法起作用,那么您必须提供实际的索引,即:

user_grades.0.grade_id

另见 Cookbook > Views > Helpers > Form > Creating Inputs for Associated Data