开发者

CakePHP not showing my form errors

开发者 https://www.devze.com 2023-03-31 11:58 出处:网络
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.

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' =&开发者_运维问答gt; __('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?


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.


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号