1

我有一个登录表单。我想用notempty()规则验证字段。我想一次显示所有错误消息(其中字段 a 为空)。在我的情况下,这不会发生。

我的代码是:

if(empty($this->data['Employee']['username'])){
    $this->Employee->invalidate('username','Please Enter User Name');
}
else if(empty($this->data['Employee']['password'])){
    $this->Employee->invalidate('password','Please Enter Password');
}
else if(empty($this->data['Employee']['project_id'])){
    $this->Employee->invalidate('project_id','Please Select Any Project.');
    $this->redirect(array('controller'=>'employees','action' => 'login'));
}

登录.ctp 文件:

<?php echo $this->Form->create('Employee', array('action' => 'login'));?> 
<h2>Login</h2> <div id="login-area"> 
<?php echo $this->Form->input('username',array('div'=>false,'id'=>'loginusername'));?> 
<?php echo $this->Form->input('password',array('div'=>false,'type'=>'password','id'=>'passw‌​ord'));?> 
<?php echo $this->Form->input('project_id',array('id'=>'projects','empty'=>'Select'));?> 
<?php echo $this->Form->end(__('submit',true));?>

控制器功能:

function login(){
        $this->layout = 'login'; //pr($this->data);exit;

        if(isset($this->data['Employee'])){
            if(empty($this->data['Employee']['username'])){
                $this->Employee->invalidate('username','Please Enter User Name');
            }
            else if(empty($this->data['Employee']['password'])){
                $this->Employee->invalidate('password','Please Enter Password');
            }

            else{
                //$project_id = $this->data['Employee']['project_id'];
                $result=$this->Employee->findByUsername($this->data['Employee']['username']);
                $pwd=$result['Employee']['password'];echo $pwd; 
                $emp_id =$result['Employee']['id'];
                if($pwd==md5($this->data['Employee']['password'])){
                    //$this->Session->write('project_id',$project_id);
                    $this->Session->write('emp_id',$emp_id);
                    $this->redirect(array('action' => 'index'));
                }

                else{
                    $this->Session->setFlash(__('Please enter valid username And password to login', true));
                }
            }
        }

如何在提交表单时一起显示所有错误消息?

4

2 回答 2

2

如果您在控制器中进行验证,应该看起来像这样。

if(empty($this->data['Employee']['username'])){
    $this->Employee->invalidate('username','Please Enter User Name');
}
if(empty($this->data['Employee']['password'])){
    $this->Employee->invalidate('password','Please Enter Password');
}

if($this->Employee->validates()) {
    // do your login stuff
}
于 2013-01-02T11:15:05.207 回答
0

您应该将验证放在您的模型类中,CakePHP 可以开箱即用地处理所有这些。

使用 CakePHP 表单助手在您的视图中呈现输入框,并且在提交无效表单时将自动呈现错误消息。

在此处查看模型验证的详细信息:http: //book.cakephp.org/2.0/en/models/data-validation.html

例如在您的模型类中

public $validate = array(
    'username' => array(
        'rule'    => 'notEmpty',
        'message' => 'Please enter username'
    )
);

在您看来(在以 开头的表单内$this->Form->create()

<?php echo $this->Form->input('username'); ?>
于 2013-01-02T09:14:49.730 回答