代码之家  ›  专栏  ›  技术社区  ›  BrynJ

CodeIgniter表单验证-如何在成功后取消设置表单值?

  •  32
  • BrynJ  · 技术社区  · 15 年前

    我意识到这个请求与CI文档中提供的示例(建议单独的“成功”页面视图)不符,但我希望在成功提交表单后重用给定的表单视图-显示成功消息,然后显示空白表单。我尝试过几种方法,但没有成功地清除验证集值(未设置 $_POST ,将规则/字段设置为空数组并重新运行验证)。

    我可以重定向到同一个页面,但是随后我必须设置一个会话变量来显示一条成功消息——这是一种混乱的方法。

    有什么办法能最好地实现上述目标吗?

    8 回复  |  直到 15 年前
        1
  •  34
  •   Teej    15 年前

    重定向到自身。这样,就没有运行任何提交…这也为您提供了一种显示flash_数据的方法。

        $this->load->library('form_validation');
    
        $this->form_validation->set_rules('firstname', 'First Name', 'required');
        $this->form_validation->set_rules('surname', 'Sur Name', 'required');
    
        if ($this->form_validation->run() === TRUE)
        {
                        // save data
    
            $this->session->set_flashdata('message', 'New Contact has been added');
            redirect(current_url());
        }
    
        $this->load->view('contacts/add', $this->data);
    
        2
  •  12
  •   insertusernamehere    12 年前

    另一个解决方案,扩展库 CI_Form_validation . 财产 $_field_data 是受保护的,因此我们可以访问它:

    class MY_Form_validation extends CI_Form_validation {
    
        public function __construct()
        {
            parent::__construct();
        }
    
        public function clear_field_data() {
    
            $this->_field_data = array();
            return $this;
        }
    }
    

    并调用新方法。这样,您就可以在会话中不存储数据的情况下传递数据。

        class Item extends Controller
        {
            function Item()
            {
                parent::Controller();
            }
    
            function add()
            {
                $this->load->library('form_validation');
                $this->form_validation->set_rules('name', 'name', 'required');
    
                $success = false;
    
                if ($this->form_validation->run())
                {
                    $success = true;
                    $this->form_validation->clear_field_data();
                }
    
                $this->load->view('item/add', array('success' => $success));
            }
        }
    
        3
  •  7
  •   bschaeffer    15 年前

    向视图传递一个真/假变量,该变量有条件地设置窗体的值。

    管制员

    if($this->form_validation->run())
    {
        $data['reset'] = TRUE;
    }
    else
    {
        $data['reset'] = FALSE:
    }
    
    $this->load->view("form", $data);
    

    视图:

    <input type="text" name="email" value="<?php echo ($reset) ? "" : set_value('email'); ?>" />
    
    <input type="text" name="first_name" value="<?php echo ($reset) ? "" : set_value('first_name'); ?>" />
    
        4
  •  4
  •   Stephen Curran    15 年前

    set_value函数从表单验证对象而不是从$_post数组中获取其值。表单验证对象将其发布值的副本存储在一个名为$\u field\u data的变量中。

    这是一个黑客程序,但在成功提交后可以清除此变量:

    class Item extends Controller
    {
        function Item()
        {
            parent::Controller();
            $this->load->model('item_model');
        }
    
        function add()
        {
            $this->load->library('form_validation');
            $this->form_validation->set_rules('name', 'name', 'required');
    
            $success = false;
    
            if ($this->form_validation->run())
            {
                $this->item_model->add_item($this->input->post('name'));
                $success = true;
    
                // Look away now. Hack coming up!
                // Clear the form validation field data
                $this->form_validation->_field_data = array();
            }
    
            $this->load->view('item/add', array('success' => $success));
        }
    }
    
        5
  •  0
  •   dt teja    11 年前

    希望这会有所帮助。最后,我理解了扩展图书馆的整个概念。你要做的就是
    步骤1 :在此目录中,“application/libraries/”创建名为“my_form_validation.php”的文件,并使用以下php代码

    <?php if (!defined('BASEPATH')) exit('No direct script access allowed.');
    class MY_Form_validation extends CI_Form_validation {
    
     public function MY_Form_validation() {
        parent::__construct();
      }
    
      public function unset_field_data()
        {    
            unset($this->_field_data);    
        }
    }
    

    第2步: 然后使用函数“ unset_field_data() “在你的控制器里。例如如下:

        if ($this->form_validation->run())
        {
            $this->item_model->add_item($this->input->post('name'));
            $success = true;
    
            $this->form_validation->unset_field_data();
        }
    
        6
  •  0
  •   quinny    8 年前

    我发现在哪里:

    1. 构成页面的多个CI视图部分及其控制器方法执行验证
    2. 其中一个验证失败,并在最后一页刷新后产生错误,例如输入字段值格式错误或键入错误。

    只清除验证规则数组是不够的,还需要清除验证错误数组。

    为此,我向system/libraries/form_validation.php添加了一个方法,如下所示:

    public function clear_rules()
    {
        $this->_error_array = array();
        $this->_field_data = array();
        return $this;
    }
    

    返回$This is important if you want to chain your form validation methods.

        7
  •  0
  •   Bhunesh Satpada    8 年前

    在较新版本3.x中,要清除设置值, $_POST=array() $this->_field_data = array(); 在里面 MY_Form_validation.php 图书馆。尝试

    Clear form data after success codeigniter using php not jquery

        8
  •  0
  •   pgee70    7 年前

    来自d5avard的答案是错误的,CI表单验证应该将规则数组解析为它:如果不这样做,则可以将表单验证与发布的数据一起使用,但不能与超程数据一起使用。

    将此文件保存为libraries/my_form_validation.php

        /**
         * Class MY_Form_validation
         * @description extension of the CI_Form_validation
         * @property CI_DB_query_builder db database driver
         * @property CI_Benchmark benchmark
         * @property CI_Input input
         * @property CI_Output output
         */
        class MY_Form_validation extends CI_Form_validation
        {
            /**
             * MY_Form_validation constructor.
             * @param array $rules
             */
            function __construct($rules = array())
            {
                parent::__construct($rules);
                $this->_error_prefix        = '<div class=""><p>';
                $this->_error_suffix        = '</p></div>';
            }
    
            /**
             * Resets the form validation class for multiple runs.
             * @param array $rules
             */
            public function initialise($rules = array())
            {
                if (count($rules) == 0 )
                {
                    require (APPPATH.'config'.DIRECTORY_SEPARATOR.'form_validation.php');
                    $this->_config_rules = $config;
                }
                else
                {
                    $this->_config_rules = $rules;
                }
                $this->_field_data          = array();
                $this->_error_array         = array();
                $this->_error_messages      = array();
                $this->_error_prefix        = '<div class=""><p>';
                $this->_error_suffix        = '</p></div>';
                $this->error_string         = '';
            }
        }