I can not validate form in Cake PHP 3

0

Hello, I'm new to CakePHP 3 and I'm having problems validating my form login.ctp, error: undefined variable users in login.ctp.

I would appreciate if someone could help me validate;

login.ctp :

<br>
<div class="index large-4 medium-5  large-offset-4 medium-offset-4 columns">
    <div class="panel">
        <h2 class="text-center">Login</h2>
        <?= $this->Form->create($user); ?>

             <?php

            echo $this->Form->input('email');
            echo $this->Form->input('password');
                ?>

            <?= $this->Form->submit('Login', array('class' => 'button')); ?>

        <?= $this->Form->end(); ?>
    </div>
</div>  

login function in UsersController.php :

public function login()
    {           
        if($this->request->is('post'))
        {
            $user = $this->Auth->identify();

            if($user)
            {
                $this->Auth->setUser($user);
                return $this->redirect(['controller' => 'comentario']);
            }

            // Erro no Login

            $this->Flash->error('Erro de autenticação');
        }



    }
    
asked by anonymous 21.07.2016 / 03:07

1 answer

0

In your View, place this validation at the beginning of the code. This variable will come from the controller when the login is invalid and the validation will populate the fields with the $ user variable.

<?php if (empty($user)) {
    $user = 'User';
} ?>
<div class="index large-4 medium-5 large-offset-4 medium-offset-4 columns">
    <div class="panel">
        <h2 class="text-center">Login</h2>
        <?= $this->Form->create($user); ?>

            <?php
                echo $this->Form->input('email');
                echo $this->Form->input('password');
            ?>

            <?= $this->Form->submit('Login', array('class' => 'button')); ?>

        <?= $this->Form->end(); ?>
    </div>
</div>  
    
22.07.2016 / 20:57