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

前端 未结 8 705
天涯浪人
天涯浪人 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));
        }
    }
    
    0 讨论(0)
  • 2020-12-23 21:21

    Another solution, extend the library CI_Form_validation. The property $_field_data is protected, so we can acces it:

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

    And call the new method. This way, you can pass data without storing data in session.

        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));
            }
        }
    
    0 讨论(0)
  • 2020-12-23 21:30

    The answer from d5avard is wrong, the CI form validation should have the rules array parsed to it: If you don't do this you can use form validation with posted data, but not with over-ride data.

    save this file as 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         = '';
            }
        }
    
    0 讨论(0)
  • 2020-12-23 21:31

    Hope this would be helpful. Finally I understand the whole concept of the extending the library. All you need to do is
    Step 1: In this directory "application/libraries/" create a file named "MY_Form_validation.php" with the following php code

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

    Step 2: Then use the function "unset_field_data()" in you controller. for example below:

        if ($this->form_validation->run())
        {
            $this->item_model->add_item($this->input->post('name'));
            $success = true;
    
            $this->form_validation->unset_field_data();
        }
    
    0 讨论(0)
  • 2020-12-23 21:34

    Redirect to itself. This way, no submissions have been run... This also gives you a way to show the 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);
    
    0 讨论(0)
  • 2020-12-23 21:35

    Pass a TRUE/FALSE variable to your views that conditionally sets the values of the form.

    The Controller

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

    The View:

    <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'); ?>" />
    
    0 讨论(0)
提交回复
热议问题