I cannot validate form in Cake PHP 3

Asked

Viewed 143 times

0

Hello, I’m new to Cakephp 3 and am having problems validating my login.ctp form, error: Undefined variable users in login.ctp.

I’d appreciate it 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');
        }



    }
  • This "<?=" statement, could you replace it with "<? php?? Might be being simplistic, but it would be my suggestion.

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 fill 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>  

Browser other questions tagged

You are not signed in. Login or sign up in order to post.