Search code examples
validationcakephp-3.4

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


I am trying to validate a multiple select field that have jquery chosen applied.

Validation is working perfectly but only problem is that validation message is not showing below the input field.

Here is my files.

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;
   }
}

I have tried to get error message and got following:

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

                        )

                )

        )

But this error is not showing below the input field. Any help will be appreciated.


Solution

  • You are not using the correct naming scheme for the form control, you cannot use [], if you want the form helper magic to work, then you must supply the actual index, ie:

    user_grades.0.grade_id
    

    See also Cookbook > Views > Helpers > Form > Creating Inputs for Associated Data