【问题标题】:how to display validation error under the each input field on redirect in codeigniter如何在codeigniter中重定向的每个输入字段下显示验证错误
【发布时间】:2017-01-20 11:32:53
【问题描述】:

我正在验证 codeigniter 中的表单。如果用户未正确填写表单,则页面不应重定向到任何其他页面,而是在每个输入字段上显示错误。这是我的表单代码:

<?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>

这是我的方法代码:

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());
            }
    }

}

问题是通过使用 set_flashdata 我不能使用 form_error 函数和 set_value 函数。这个问题还有其他解决方法吗???

【问题讨论】:

  • 你有没有试过任何答案,如果他们工作不接受他们
  • 您不需要设置闪烁消息来获取验证错误。 Codeigniter 自己做这件事。您必须在验证失败后加载表单。

标签: php jquery forms codeigniter codeigniter-3


【解决方案1】:

在你输入的地方 form_error() 你需要加载表单帮助器

您可以自动加载库等 config/autoload.php

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

表单错误示例

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

如图https://www.codeigniter.com/userguide3/libraries/form_validation.html#changing-the-error-delimiters

控制器文件名 Detail.php 第一个字母只能是大写的类和文件名

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

}

查看

<?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();?>

【讨论】:

    【解决方案2】:

    您应该避免在验证失败后重定向。 您有两个选项可以恢复验证错误:

    1. 加载页面:

      $this->load->view('页面名称);

    2. 您可以加载页面表单特定功能的视图。例如,如果您有索引功能来加载表单,那么您可以这样做。

      $this->index();

    要单独填充验证错误,您可以像这样显示它们:

    <?= form_error('your input field name') ?>
    

    或在表单顶部显示所有错误您可以这样做。

    <?php echo validation_errors(); ?>
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-26
      • 2021-07-17
      • 2016-04-18
      • 2012-06-29
      • 1970-01-01
      相关资源
      最近更新 更多