mhmpl
mhmpl

Reputation: 1043

CakePHP not showing my form errors

I'm trying to create a login form for my web application.
Form validation errros are not showing even though I'm using the $validate Array.

user.php

public $validate = array(
    'email' => array(
        'notEmpty' => array(
            'rule' => 'notEmpty',
            'message' => 'notEmpty',
            'required' => true
        ),
        'isEmail' => array(
            'rule' => 'email'
        ),
        'isUnique' => array(
            'rule' => 'isUnique'
        )           
    ),
    'password' => array(
        'notEmpty' => array(
            'rule' => 'notEmpty'
        ),
        'minLength' => array(
            'rule' => array('minLength', 8)
        )
    )
);

I can't see an error in my user model, so I show you my controller and my view.

users_controller.php

class UsersController extends AppController {
      public $name = 'Users';
      public $helpers = array(
       'Form'
      );


public function login() {
    if(!empty($this->data)) {
        if ($this->Auth->user() != null) {
            $this->Session->setFlash('You are now logged in.', 'flash/success');
            $this->redirect('/');
        } else {
            $this->Session->setFlash('You could not get logged in. Please see errors below.', 'flash/error');
        }
    }
}

login.ctp

echo $this->Form->create('User', array('action' => 'login'));
echo $this->Form->input('User.email', array(
    'label' => __('email address:', true),
    'error' => array(
        'notEmpty' => __('Email address must not be blank.', true),
        'isEmail' => __('Email address must be valid.', true),
    )
));
echo $this->Form->input('User.password', array('label' => __('password:', true)));
echo $this->Form->end('Log in');

I hope you can help me. I can't find my mistake since hours. Is there maybe a component or an helper which I need to include?

Upvotes: 1

Views: 1468

Answers (2)

Anh Pham
Anh Pham

Reputation: 5481

put echo $this->Session->flash('auth'); before form->create. You don't have to validate login form, Auth will take care of that for you. Read the cookbook: http://book.cakephp.org/view/1250/Authentication

Since you are using Auth, the minLength validation for password is useless.

Upvotes: 1

JJJ
JJJ

Reputation: 33163

Validation doesn't occur automatically unless you're saving into the database. Change the first line of the login method in the controller to

if( !empty( $this->data ) && $this->User->validates() ) { 
    ...

Upvotes: 1

Related Questions