1

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?

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

OK thank you! Is it possible to deactivate these required symbols for the login form?
With default layout and css files, CakePHP is adding the red star (*) symbol.
you can change the css, or remove the notEmpty rule.
1

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() ) { 
    ...

1 Comment

That doesn't change anything. Do I maybe have to send the validation errors to the form?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.