2011-08-30 68 views
1

我想为我的web应用程序创建一个登录表单。
即使我使用$validate阵列,也不会显示表单验证errros。CakePHP不显示我的表单错误

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) 
     ) 
    ) 
); 

我看不到我的用户模型中的错误,所以我会告诉你我的控制器和我的观点。

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'); 

我希望你能帮助我。几小时后我找不到我的错误。是否有可能需要包含组件或助手?

回答

1

echo $this->Session->flash('auth');form->create之前。您不必验证登录表单,Auth将为您处理此问题。阅读食谱:http://book.cakephp.org/view/1250/Authentication

由于您使用的是Auth,所以对密码的minLength验证是无用的。

+0

好的,谢谢!是否可以停用登录表单的这些必需符号? – mhmpl

+0

需要什么符号? –

+0

使用默认布局和css文件,CakePHP添加红色星号(*)符号。 – mhmpl

1

验证不会自动发生,除非您保存到数据库中。在控制器中的登录方法的第一行更改为

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

这并没有改变任何东西。我可能需要将验证错误发送到表单吗? – mhmpl