CakePHP 3.x: How Can I automatically show the validation errors into view file

I am getting validation errors in controller (from rules set in the respective model file) , but unable to make them automatically display below their respective input fields in view file.(as those were shown in Cake 2.0 versions)

Here is Controller function i have used for editing a user:
public function edit($user_id = null) {
$user = $this->Users->get($user_id);
if ($this->request->is(‘post’)) {
$user= $this->Users->patchEntity($user, $this->request->data);
if (empty($user->errors())) {
$this->Users->save($user);
} else {
$this->set(‘errors’, $user->errors());
}
} else {
$this->request->data = $user;
}
$this->set(‘user’,$user);
}

VIEW FILE:

Add User
<?php echo $this->Flash->render(); ?>
<?php echo $this->Form->create($user, array('class' => '', 'enctype' => 'multipart/form-data')); ?>
<?php echo $this->Form->input('name', array('class' => 'form-control', 'type' => 'text', 'label' => 'Name', 'required' => false, 'error' => true));
<?php echo $this->Form->input('email', array('class' => 'form-control', 'type' => 'text', 'label' => 'Email address', 'required' => false, 'error' => true)); ?>
<?php echo $this->Form->input('password', array('class' => 'form-control', 'type' => 'text', 'label' => 'Password', 'required' => false, 'error' => true, 'type' => 'password')); ?>
<?php echo $this->Form->input('confirm_password', array('class' => 'form-control', 'type' => 'text', 'label' => 'Retype Password', 'required' => false, 'error' => true, 'type' => 'password')); ?>
<?php echo $this->Form->input('image', array('class' => 'form-group', 'type' => 'file', 'required' => false, 'error' => true)); ?>

Example block-level help text here.

<?php echo $this->Form->button('Sumbit', array('class' => 'btn btn-info')); echo $this->Form->end(); ?>
        </div>
    </section>

</div>

UserTable.php

<?php namespace App\Model\Table; use Cake\ORM\Table; use Cake\Validation\Validator; //use Cake\ORM\RulesChecker; class UsersTable extends Table { public function validationDefault(Validator $validator) { return $validator ->notEmpty( 'username', 'Please Enter your Username.' ) ->notEmpty( 'name', 'Please Enter your name.' ) ->notEmpty( 'email', 'Please Enter your Email Address.' ) ->add( 'email', ['unique' => [ 'rule' => 'validateUnique', 'provider' => 'table', 'message' => 'Email Already Exists'] ] ) ->notEmpty('password', 'Please Enter your password.') ->notEmpty('confirm_password', 'Please ReConfirm your password.') ; } function initialize(array $config) { parent::initialize($config); $this->addBehavior('Timestamp'); } ---- so how to show those errors automatically in view file below the input fields?

You should to apply this where you want to show your error

<?= $this->Flash->render('auth') ?> <?= $this->Flash->render();?>

You dont need to do this for errors appears below the input and to repopulate data if validation fail.

hi mokiwaa: , thanks for answer.
but we are unable to make the errors display automatically below the respective form input fields.(as in cake 2.0 versions). any thing i need to add for showing those??

You can do this.i just modified your code a little bit:

public function edit($user_id = null) {
$user = $this->Users->get($user_id);
if ($this->request->is(‘post’)) {
$user= $this->Users->patchEntity($user, $this->request->data);
if ($this->Users->save($user)){
// do whatever you want after saving your data like redirecting to another action or setting flash messages.
}
}
$this->set(‘user’,$user);
}

Note: Calling the save method automatically validates the inputs…u dont need to call the error() method explicitly

if validation fails then your edit.ctp will be rendered with the form data as well as error messages