【问题标题】:CakePHP not showing my form errorsCakePHP 没有显示我的表单错误
【发布时间】:2011-08-30 07:33:04
【问题描述】:

我正在尝试为我的网络应用程序创建一个登录表单。
即使我使用 $validate 数组,表单验证错误也没有显示。

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

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

【问题讨论】:

    标签: forms validation cakephp


    【解决方案1】:

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

    由于您使用的是身份验证,因此密码的 minLength 验证是无用的。

    【讨论】:

    • 好的,谢谢!是否可以为登录表单停用这些必需的符号?
    • 使用默认布局和 css 文件,CakePHP 正在添加红色星 (*) 符号。
    • 你可以改变css,或者去掉notEmpty规则。
    【解决方案2】:

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

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

    【讨论】:

    • 这不会改变任何事情。我是否必须将验证错误发送到表单?
    猜你喜欢
    • 2014-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-02
    • 1970-01-01
    相关资源
    最近更新 更多