How can I display the validation error messages in the Login view of the UsersController?

How can I display the validation error messages in the Login view of the UsersController?

Hi, I hope someone can help me. How can I get to show the validation errors when entering wrong data fields of the login form? in add, edit when submitting the form are displayed correctly.

Users/login.ctp

			<h1 class="h4 mb-3 font-weight-bold">Acceso administración</h1>
			<?= $this->Form->create() ?>
			<fieldset>
			<?= $this->Form->control('username',['class' =>'form-control mb-2','placeholder' => 'Nombre de usuario','label' => false ,'escape' => false]);?>


			<?= $this->Form->control('password',['class' =>'form-control mb-2','placeholder' => 'Contraseña','label' => false ,'escape' => false]);?>
		
			<?= $this->Form->button(__('Acceder'),['class' => 'btn btn-lg btn-success w-100 my-3'])?>
			<?= $this->Form->end() ?>
			</fieldset>
			</div>
		</div>

UsersTable.php

class UsersTable extends Table
{

 public function validationDefault(Validator $validator){
    $validator
        ->requirePresence('username', true)
        ->notEmptyString('username', 'El nombre de usuario es obligatorio', false)
        ->add('username', [
                'unique' => [
                    'rule' => 'validateUnique',
                    'provider' => 'table',
                    'message' => 'El nombre de usuario ya existe'
                ]
    ]);

    $validator
        ->allowEmptyString('password', 'La contraseña es obligatoria', false)
        ->add('password', [
            'length' => [
                'rule' => ['minLength', 5],
                'message' => 'La contraseña debe tener mínimo 5 caracteres',
            ]
    ]);
 

    return $validator;
}

Action login in UsersController.php

public function login(){

    $this->viewBuilder()->setLayout('publico');

    if ($this->request->is('post')) {
        $user = $this->Auth->identify();
        
        if ($user) {
            $this->Auth->setUser($user);
            $this->Flash->success(__('El administrador ha iniciado sesión correctamente'));
            return $this->redirect($this->Auth->redirectUrl());
        }

        $this->Flash->error(__('Nombre de usuario o contraseña incorrectos, inténtalo de nuevo.'));
    }
}

Thank you very much and I hope someone can help me

The Form helper will generally detect and render error messages but it needs access to your Entity to do that work.

Typically that will happen something like this:

public function login(){

    $entity = new Entity([]);

        if ($this->request->is('post') {
            $this->Model->patchEntity($entity, $this->request->getData();
            // other code here redirects on successful process
        }

        // now entity is either new and empty or 
        // contains the user's posted input values and 
        // error messages created during `patch` or `save` processes
        $this->set('entity', $entity);
    }
}

Now with the entity (containing data and error details) available in your template, you will need to give it to the Form helper.

Users/login.ctp
// Form->create() gets the entity as an argument

echo $this->Form->create($entity);

   echo $this->Form->control('username',);
   echo $this->Form->control('password');
   echo $this->Form->button(__('Acceder'));

echo $this->Form->end();

Now, if the control names match the entity property names, everything should work.

So, your going to need to look at what $user = $this->Auth->identify(); delivers to you. If it is an entity you just need to follow the general pattern outlined above and the Form::control() calls should detect and render validation errors.