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

[eluser]intoitdaily[/eluser]
Alright, I'm not so new to codeigniter, but this is the first time I've encountered this issue. So I'm building a form, like normal, in codeigniter. And I noticed that for all the input fields that are to be pre-populated in case of an error found in one of the entries, code igniter won't pre-populate them unless they were required in some way set by the form_validation->set_rules method in its controller.

I don't understand why these features were built in this way. it doesn't make sense. the error fields are not the only ones that should be pre-populated. All the input fields, dropdowns, checkboxes, textareas, etc, should be pre-populated in case any of the input fields encounter an error. My question is, is there a way around this? Maybe I could create my own error_callback function, but just have a pass or something inside it. maybe then they'd all be prepopulated?

Has anyone noticed this before? Am I doing something wrong here?
#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.
#3

[eluser]Dam1an[/eluser]
Can you not just set a rule with an empty string as the rule? Then it's aware of it, and can repopulate it, or you could always do something like trim or xss_clean, they're always good to do
#4

[eluser]intoitdaily[/eluser]
That works great! Thanks for that little trick!
#5

[eluser]Dam1an[/eluser]
You're welcome Smile
Thats something that everyone picks up at some point, and it's very useful Smile
#6

[eluser]intoitdaily[/eluser]
That's what I was thinking. I was like, "I can't be the first person who's encountered this problem... and CodeIgniter usually has built-in methods that handle common tasks like this." But I wasn't aware of it, yet. So I tried the forums. And coincidentally, my original assumption was correct. As displayed above in my second post in this thread, adding your own callback function that returns True works! It just goes to show that CodeIgniter is <i>so</i> versatile and dynamic you can solve one problem many different, logical ways... Love CodeIgniter

I'm curious, though. Why wouldn't CodeIgniter's form helper/validator automatically pre-populate input fields, even though they might not have any set rules applied to them? It seems that more often than not, developers using CodeIgniter would prefer <i>all</i> of the input fields to be pre-populated, regardless. Can anyone answer this for me?
#7

[eluser]Adam Griffiths[/eluser]
The form validation library only re-populates form fields if i's told about the fields in such a way that it is supposed to, i.e. using the set_rules method.

If this wasn't the case, then CodeIgniter would need to go through all the post data, and this could be tainted. I've no doubt that Ellis Lab may add this in to CodeIgniter if they feel it will add more value to the framework.

I always add an xss_clean rule or another rule of some sort so everything can be re-populated.
#8

[eluser]Maglok[/eluser]
You can just leave the third parameter empty to not check on anything with that field. xss_cleaning because you need to do something is kinda odd.

There is another issue with this though, at least in my opinion. Let's say it's an edit form, you grab data from a database, display it, check new values and update the database.

You'd determine which value the page should show like this (in bambooinvoice and other CI apps at least):
Code:
set_value('something')? $data['something']=set_value('something'):$data['something'] = $row->something;
The set_value can't see if you want a field to be empty or not though. This leaves room for the following scenario: Someone edits an exsisting thing, then empties a required field and a non-required field. It will then reload the page stating the required field is required AND it will repopulate both fields. Even if the non-required field is meant to stay empty.
#9

[eluser]yudahebat[/eluser]
intoitdaily , can you upload the code that you edit with the little trick?
I wanna see it, I have a same problem like you...
but I dont understand about the trick.. my english very bad...
#10

[eluser]intoitdaily[/eluser]
yudahebat,

The Controller:
Code:
function myform()
    {
        $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', '', '');
        $this->form_validation->set_rules('field3', '', '');
        $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 {
            // process post data (insert into database, send email, etc)
        }
    }

The View:
Code:
&lt;?= form_open('myform'); ?&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;




Theme © iAndrew 2016 - Forum software by © MyBB