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

时间:2010-05-10 10:57:24

标签: php validation forms codeigniter

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

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

任何想法如何最好地实现上述目标?

8 个答案:

答案 0 :(得分:34)

重定向到自己。这样,没有提交任何提交...这也为您提供了一种显示flash_data的方法。

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

答案 1 :(得分: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));
        }
    }

答案 2 :(得分:7)

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

答案 3 :(得分:4)

set_value函数从Form_validation对象中获取其值,而不是从$ _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));
    }
}

答案 4 :(得分:0)

希望这会有所帮助。最后,我理解了扩展库的整个概念。你需要做的就是
第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);    
    }
}

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

答案 5 :(得分:0)

我发现在哪里:

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

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

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

    如果您想链接表单验证方法,那么返回$这很重要。

答案 6 :(得分:0)

在较新版本3.X中,要清除 $_POST=array() 库中的set_value,$this->_field_data = array();MY_Form_validation.php。试试

Clear form data after success codeigniter using php not jquery

答案 7 :(得分:0)

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         = '';
        }
    }