donglei2022 2017-01-20 11:32
浏览 45

如何在codeigniter中重定向的每个输入字段下显示验证错误

I am validating a form in codeigniter. If the user did not fill in the form correctly, then the page should not redirect to any other page, but display errors on each input field. Here is my code of form:

<?php  echo $this->session->flashdata('errors');?>
<form action="<?php echo base_url() ?>detail/travel" method="post">

<input type="text" name="departure">
<input type="text" name="destination">
<input type="text" name="name">
<input type="text" name="cell">

</form>

and here is my method code:

function search_travel(){
                if($_POST){

                    $config = array(
                    array(
                    'field'=>'departure',
                    'label'=>'departure',

                'rules'=>'trim|required|alpha'
                ),
                array(
                'field'=>'destination',
                'label'=>'destination',
                'rules'=>'trim|required|alpha'
                ),
                array(
                'field'=>'name',
                'label'=>'name',
                'rules'=>'trim|required|alpha'
                ),
                array(
                'field'=>'cell',
                'label'=>'cell no',
                'rules'=>'trim|required'
                )
            );
        $this->load->library('form_validation');
        $this->load->helper(array('form', 'url'));
        $this->form_validation->set_rules($config);

        if($this->form_validation->run() == FALSE){
            $this->session->set_flashdata('errors', validation_errors());
             redirect(base_url());
            }
    }

}

The problem is that by using set_flashdata I can't use form_error function and set_value function. Is there any other solution to this problem???

  • 写回答

2条回答 默认 最新

  • duanruanxian5028 2017-01-20 12:03
    关注

    Under your input place form_error() you need to load the form helper though

    You can autoload libraries etc config/autoload.php

    $autoload['helper'] = array('form', 'html', 'url');
    

    Form Error Example

    <input type="text" name="departure">
    <?php echo form_error('departure', '<div class="error">', '</div>'); ?>
    
    <input type="text" name="destination">
    <?php echo form_error('destination', '<div class="error">', '</div>'); ?>
    
    <input type="text" name="name">
    <?php echo form_error('name', '<div class="error">', '</div>'); ?>
    
    <input type="text" name="cell">
    <?php echo form_error('cell', '<div class="error">', '</div>'); ?>
    

    As shown here https://www.codeigniter.com/userguide3/libraries/form_validation.html#changing-the-error-delimiters

    Controller filename Detail.php first letter only must be upper case class and file name

    <?php  
    
    class Detail extends CI_Controller {
    
      // You can autoload helpers and libraries etc if you need to, 
    
       public function __construct() {
          parent::__construct();
          $this->load->helper('form');
          $this->load->library('form_validation');
       }
    
       public function index() {
          $this->load->view('your_form_view');
       }
    
       function search_travel() {
           $this->form_validation->set_rules('departure', 'departure', 'trim|required');
           $this->form_validation->set_rules('destination', 'destination', 'trim|required');
           $this->form_validation->set_rules('name', 'name', 'trim|required');
           $this->form_validation->set_rules('cell', 'cell', 'trim|required');
    
           if ($this->form_validation->run() == FALSE) {
              // Error
              $this->load->view('your_form_view');
           } else {
              // Success
              redirect(base_url('another/controller'));
           }
       }
    
    }
    

    View

    <?php echo form_open('detail/search_travel');?>
    
    <input type="text" name="departure">
    <?php echo form_error('departure', '<div class="error">', '</div>'); ?>
    
    <input type="text" name="destination">
    <?php echo form_error('destination', '<div class="error">', '</div>'); ?>
    
    <input type="text" name="name">
    <?php echo form_error('name', '<div class="error">', '</div>'); ?>
    
    <input type="text" name="cell">
    <?php echo form_error('cell', '<div class="error">', '</div>'); ?>
    
    // Submit button
    
    <?php echo form_close();?>
    
    评论

报告相同问题?