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

Php Codeigniter表单验证-成功后如何取消设置表单值?,php,validation,forms,codeigniter,Php,Validation,Forms,Codeigniter,我意识到此请求与CI文档中提供的示例(建议使用单独的“成功”页面视图)背道而驰,但我希望在成功提交表单后重新使用给定的表单视图-显示成功消息,然后显示空白表单。我尝试了几种清除验证集值的方法,但均未成功(取消设置$\u POST,将规则/字段设置为空数组,然后重新运行验证) 我可以重定向到同一个页面,但是我必须设置一个会话变量来显示一条成功消息——这是一种混乱的方法 如何最好地实现上述目标,您有什么想法吗?向视图传递一个TRUE/FALSE变量,该变量有条件地设置表单的值 控制器 if($thi

我意识到此请求与CI文档中提供的示例(建议使用单独的“成功”页面视图)背道而驰,但我希望在成功提交表单后重新使用给定的表单视图-显示成功消息,然后显示空白表单。我尝试了几种清除验证集值的方法,但均未成功(取消设置
$\u POST
,将规则/字段设置为空数组,然后重新运行验证)

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


如何最好地实现上述目标,您有什么想法吗?

向视图传递一个TRUE/FALSE变量,该变量有条件地设置表单的值

控制器

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

set\u value函数从Form\u验证对象而不是从$\u POST数组获取其值。Form_validation对象将其自己的发布值副本存储在名为$_field_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));
    }
}

重定向到自身。这样,就不会运行任何提交。。。这也为您提供了一种显示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);

另一个解决方案是扩展库
CI\u Form\u validation
。属性
$\u字段\u数据
受保护,因此我们可以访问它:

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

希望这会有帮助。最后,我理解了扩展库的整个概念。您所需要做的就是
步骤1:在此目录“application/libraries/”中,使用以下php代码创建一个名为“MY_Form_validation.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);    
    }
}
我发现:

  • 有多个CI视图分区组成页面,它们的控制器方法执行验证
  • 其中一个验证失败,并在上次页面刷新后产生错误,例如输入字段值的格式或类型不正确
  • 仅清除验证规则数组是不够的,还需要清除验证错误数组

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

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

    如果要链接表单验证方法,返回$this很重要。

    在较新的3.X版中,要清除set\u值,
    $\u POST=array()
    $this->\u field\u data=array()
    我的表单验证.php
    库中。试一试


    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         = '';
            }
        }
    
    /**
    *分类我的表格验证
    *@description扩展CI_表单_验证
    *@property CI\u DB\u query\u builder数据库驱动程序
    *@property CI_基准
    *@property CI_输入
    *@property CI_输出
    */
    类MY_Form_验证扩展了CI_Form_验证
    {
    /**
    *我的表单验证构造函数。
    *@param数组$rules
    */
    函数构造($rules=array())
    {
    父项::_构造($rules);
    $this->_error_prefix='';
    $this->_error_suffix='

    '; } /** *为多次运行重置表单验证类。 *@param数组$rules */ 公共函数初始化($rules=array()) { 如果(计数($rules)==0) { require(APPPATH.config.DIRECTORY_SEPARATOR.form_validation.php); $this->_config_rules=$config; } 其他的 { $this->_config_rules=$rules; } $this->_field_data=array(); $this->_error_array=array(); $this->_error_messages=array(); $this->_error_prefix=''; $this->_error_suffix='

    '; $this->error\u string=''; } }
    取消设置后应该可以工作。你能发布一些代码吗?当重定向到它自己的时候,事情是如何混乱的?你不需要“黑客”任何东西。只需将其重定向到自身,这样就不会有提交。是的,这是显而易见的方法。但OP表示他不想这样做。我在尝试将
    \u field\u data
    设置为空白数组时收到此错误:
    致命错误:无法访问受保护的属性CI\u Form\u验证::$\u field\u data
    @kjones,我们无法直接访问此变量。所以我们需要扩展库,就像这是受保护的财产一样,你不能完全用最好的方法来处理它。我们一直在寻找这个,认为这是唯一的方法。。。虽然我不喜欢在会话中存储成功/错误消息。非常感谢您的智慧之珠:)此解决方案的不幸之处在于,如果重定向无法正确呈现,flashdata将丢失。我们必须为客户端编写一个回调代码,以便主动确认用户收到了flashdata,可能是通过一个来自呈现的对话框的AJAX回调,尽管这仅限于JS兼容的客户端。我认为这比选择的答案要好得多,因为它在CI的边界内工作。它扩展表单验证库的方式在本质上也是非常可扩展的,这是非常OOP的。CI表单验证构造函数需要规则arrayuse
    isset($reset)
    ,这样php就不会标记未定义的变量错误。代码变为:value=”“
        /**
         * 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         = '';
            }
        }