Welcome Guest, Not a member yet? Register   Sign In
Form Validation / Pre-populated input fields
#2

[eluser]intoitdaily[/eluser]
turns out, I was right...

CodeIgniter will not pre-populate your input fields when running form validation, <i>unless</i> you set a rule for that input field in the controller. However, what if there is a field that isn't required in your form, but you still want it to be pre-populated if an error is found elsewhere in the form? Here's the work-around... You have to add your own validation function in the form of a callback ( http://ellislab.com/codeigniter/user-gui...#callbacks ) So here's what I did.

Controller
Code:
function auto()
    {
        $this->form_validation->set_error_delimiters('<span class="error">', '</span>');
        
        function pass_field() {
            return True;
    }
        
        $this->form_validation->set_rules('field1', 'Field 1', 'required|alpha');
        $this->form_validation->set_rules('field4', 'Field 4', 'required');
        $this->form_validation->set_rules('field2', '', 'callback_pass_field');
        $this->form_validation->set_rules('field3', '', 'callback_pass_field');
        $this->form_validation->set_rules('field5', 'Field 5', 'required|max_length[12]');
        
        if ($this->form_validation->run() == False) {
            $this->load->view('header');
            $this->load->view('nav');
            $this->load->view('form');
            $this->load->view('footer');
        }
        else {
            // do whatever
        }
    }

View (form)
Code:
&lt;?= form_open(); ?&gt;
    &lt;?= form_fieldset('Field Set'); ?&gt;
        <ol>
            <li><label for="field1">Field 1</label>&lt;?= form_input(array('name' => 'field1', 'size' => '25', 'value' => set_value('field1'))); ?&gt;<span style="padding-left: 10px; padding-right: 10px; color: #f00; font-size: 14px;">*</span>&lt;?= form_error('field1'); ?&gt;</li>
            <li><label for="field2">Field 2</label>&lt;?= form_input(array('name' => 'field2', 'size' => '25', 'value' => set_value('field2'))); ?&gt;<span style="padding-left: 10px; padding-right: 10px; color: #f00; font-size: 14px;">*</span>&lt;?= form_error('field2'); ?&gt;</li>
            <li><label for="field3">Field 3</label>&lt;?= form_input(array('name' => 'field3', 'size' => '25', 'value' => set_value('field3'))); ?&gt;</li>
            <li><label for="field4">Field 4</label>&lt;?= form_input(array('name' => 'field4', 'size' => '25', 'value' => set_value('field4'))); ?&gt;</li>
            <li><label for="field5">Field 5</label>&lt;?= form_input(array('name' => 'field5', 'size' => '25', 'value' => set_value('field5'))); ?&gt;<span style="padding-left: 10px; padding-right: 10px; color: #f00; font-size: 14px;">*</span>&lt;?= form_error('field5'); ?&gt;</li>
        </ol>
        &lt;?= form_submit('submit', 'Submit'); ?&gt;
    &lt;?= form_fieldset_close(); ?&gt;
&lt;?= form_close(); ?&gt;

Notice, in the view I didn't add the print error for the non-required fields. This workaround worked for me. Let me know how it works for you guys.


Messages In This Thread
Form Validation / Pre-populated input fields - by El Forum - 08-11-2009, 12:50 PM
Form Validation / Pre-populated input fields - by El Forum - 08-11-2009, 02:16 PM
Form Validation / Pre-populated input fields - by El Forum - 08-11-2009, 02:34 PM
Form Validation / Pre-populated input fields - by El Forum - 08-11-2009, 03:04 PM
Form Validation / Pre-populated input fields - by El Forum - 08-11-2009, 03:22 PM
Form Validation / Pre-populated input fields - by El Forum - 08-11-2009, 05:10 PM
Form Validation / Pre-populated input fields - by El Forum - 08-11-2009, 06:30 PM
Form Validation / Pre-populated input fields - by El Forum - 08-12-2009, 03:03 AM
Form Validation / Pre-populated input fields - by El Forum - 08-16-2009, 05:27 AM
Form Validation / Pre-populated input fields - by El Forum - 08-22-2009, 09:38 PM



Theme © iAndrew 2016 - Forum software by © MyBB