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

时间:2018-07-30 07:00:38

标签: validation cakephp-3.4

我正在尝试验证应用了选择的jquery的多重选择字段。

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

这是我的文件。

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.
                                )

                        )

                )

        )

但是此错误未在输入字段下方显示。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

您没有为表单控件使用正确的命名方案,因此不能使用[],如果您希望表单帮助符起作用,则必须提供实际的索引,即:

user_grades.0.grade_id

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