表单验证显示2个回调消息

时间:2014-06-29 12:49:32

标签: codeigniter

我正在尝试为codeigniter设置自己的回调消息。但是当我尝试自己制作时,它会显示消息codeigniter one和我的自定义消息。以下是我的错误链接显示的最高错误是我想使用的http://postimg.org/image/v9ilejmnt/

如何让它只显示我的自定义错误消息?

我也使用hmvc,因此我必须在表单验证中添加run($ this)才能使回调工作。

控制器:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login extends Controller {

   private $error = array();

   public function __construct() {
      parent::__construct();
      $this->load->library('user');
      $this->load->library('form_validation');
      $this->lang->load('common/login', 'english');
      if($this->session->userdata('user_id')) {
         redirect('dashboard');
      } else {
         return false;
         redirect('login');
      }
   } 

   public function index() {
      $this->form_validation->set_rules('username', 'Username', 'required|min_length[4]|max_length[12]|xss_clean|callback_validate');
      $this->form_validation->set_rules('password', 'Password', 'required|xss_clean');

      if($this->form_validation->run($this) == false) {

         if (array_key_exists('warning', $this->error)) {
            $data['error_warning'] = $this->error['warning'];
         } else {
            $data['error_warning'] = '';
         }

         $data['action'] = site_url('login');

         $this->load->view('common/login', $data);
      } else {
          redirect('dashboard');
      }
   }

   function validate() {
      $username = $this->input->post('username');
      $password = $this->input->post('password');

      if ($this->user->login($username, $password)) {
          return true;
      } else {
         $this->error['warning'] = $this->lang->line('error_login');
         return !$this->error;
      }
   }
}

查看

<?php echo modules::run('common/header/index');?>

<div class="container">
<div class="row">
<div class="col-lg-6 col-lg-offset-3 col-md-4 col-md-offset-4 col-sm-offset-4 col-sm-4">
<div class="panel panel-default">
<div class="panel-heading"><h2 class="panel-title">Administration Login</h2></div>
<div class="panel-body">

<?php if ($error_warning) { ?>
  <div class="alert alert-danger"><i class="fa fa-exclamation-circle"></i> <?php echo $error_warning; ?>
  <button type="button" class="close" data-dismiss="alert">&times;</button>
 </div>
<?php } ?>

<form action="<?php echo $action;?>" method="post" enctype="multipart/form-data">

<div class="form-group">
  <div class="input-group"><span class="input-group-addon"><i class="fa fa-user"></i> </span>
     <input type="text" name="username" value="" placeholder="Username"  class="form-control" size="50" />
  </div>
  <?php echo form_error('username', '<div class="text-danger">', '</div>'); ?>
</div>

<div class="form-group">
  <div class="input-group"><span class="input-group-addon"><i class="fa fa-lock"></i></span>
    <input type="password" name="password" value="" placeholder="Password"  class="form-control"/>
  </div>
     <?php echo form_error('password', '<div class="text-danger">', '</div>'); ?>
  </div>

<div class="form-group">
  <div class="text-right">
    <button type="submit" class="btn btn-primary"><i class="fa fa-key"></i> Login</button>
  </div>
</div>

</form>
</div><!--/. Panel Body -->
</div><!--/. Panel Panel Default -->
</div>
</div>
</div>
<?php echo modules::run('common/footer/index');?>

2 个答案:

答案 0 :(得分:0)

您忘记为回调设置错误消息。

$this->form_validation->set_message('validate', 'invalid username or password');
return FALSE;

或者你可以做

$this->form_validation->set_message('validate', $this->lang->line('error_login'));
return FALSE;

在您的视图文件中,您显示的是每个字段(CI表单验证消息)以及您自己的<?php echo $error_warning; ?>

的错误

您应该使用以下代码

   function validate() {
      $username = $this->input->post('username');
      $password = $this->input->post('password');

      if ($this->user->login($username, $password)) {
          return true;
      } else {
              $this->form_validation->set_message('validate', $this->lang->line('error_login'));
              return FALSE;
      }

并从视图中删除此块

<?php if ($error_warning) { ?>
  <div class="alert alert-danger"><i class="fa fa-exclamation-circle"></i> <?php echo $error_warning; ?>
  <button type="button" class="close" data-dismiss="alert">&times;</button>
 </div>
<?php } ?>
   }

并删除此

  if (array_key_exists('warning', $this->error)) {
            $data['error_warning'] = $this->error['warning'];
         } else {
            $data['error_warning'] = '';
         }

答案 1 :(得分:0)

我今天就把它搞定了。当想要使用两种验证方法时,自定义和表单验证都不能使用回调。

public function index() {
      $this->form_validation->set_rules('username', 'Username', 'required|min_length[4]|max_length[12]');
      $this->form_validation->set_rules('password', 'Password', 'required|xss_clean');

      if($this->form_validation->run($this) == false) {

         $data['title'] = $this->lang->line('heading_title');

         $data['text_heading'] = $this->lang->line('text_heading');

         if (array_key_exists('warning', $this->error)) {
            $data['error_warning'] = $this->error['warning'];
         } else {
            $data['error_warning'] = '';
         }

         $data['action'] = site_url('login');

         $this->load->view('common/login', $data);

      } else {

         if($this->validate()) {

             redirect('dashboard');

            } else {

               $data['title'] = $this->lang->line('heading_title');

               $data['text_heading'] = $this->lang->line('text_heading');

               if (array_key_exists('warning', $this->error)) {
                  $data['error_warning'] = $this->error['warning'];
               } else {
                  $data['error_warning'] = '';
               }

               $data['action'] = site_url('login');

               $this->load->view('common/login', $data);

         }
      }
   }