我有一个登录表单。我想用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'=>'password'));?>
<?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));
}
}
}
如何在提交表单时一起显示所有错误消息?