Codeigniter Form Validation - how to unset form values after success?

前端 未结 8 728
天涯浪人
天涯浪人 2020-12-23 20:36

I realise this request goes against the example provided in the CI documentation (which advises a separate \'success\' page view), but I would like to reutilise a given form

8条回答
  •  时光取名叫无心
    2020-12-23 21:20

    The set_value function fetches its value from the Form_validation object and not from the $_POST array. The Form_validation object stores its own copy of the posted values in a variable called $_field_data.

    Its a hack, but you could clear this variable after handling a successful submission :

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

提交回复
热议问题