Welcome Guest, Not a member yet? Register   Sign In
Re-populating form values on validation fail - dropdowns
#1

[eluser]EmmyS[/eluser]
I have a very simple voting form. It contains a single text field for entry ID, then five dropdowns. The only validation being done is on the text field, but if it fails, I want all of the dropdowns to keep their values.

For example, a user enters 12345 as the entry, then selects option A from all five dropdowns. (Option B is default.) If 12345 fails validation, the dropdowns should still be set to option A when the validation error is displayed. That's not happening. I'm using the form helper's form_dropdown to create the dropdowns. Any ideas? As you can see, I tried using set_value on the dropdown called q1, but it doesn't work. The validation itself does work.

Here's the code for the form:

Code:
<?php
           $hidden = array('dot_judge_id' => $this->session->userdata('dot_judge_id'));

            echo form_open('vote/create');
                $entry_code_data = array(
                    'name' => 'entry_code',
                    'id' => 'entry_code',
                    'value' => set_value('entry_code')
                );
                
                echo form_hidden($hidden);
                
                $score_options = array('1'=>'1 (lowest)', '2'=>'2','3'=>'3', '4'=>'4','5'=>'5 (highest)');
              
                ?>
                <p><label for="entry_code">Entry code: </label>&lt;?php echo form_input($entry_code_data); ?&gt;</p>
                <p><label for="q1">Question 1: </label>&lt;?php echo form_dropdown('q1', $score_options, set_value('q1')); ?&gt;</p>
                <p><label for="q2">Question 2: </label>&lt;?php echo form_dropdown('q2', $score_options, ''); ?&gt;</p>
                <p><label for="q3">Question 3: </label>&lt;?php echo form_dropdown('q3', $score_options, ''); ?&gt;</p>
                <p><label for="q4">Question 4: </label>&lt;?php echo form_dropdown('q4', $score_options, ''); ?&gt;</p>
                <p><label for="q5">Question 5: </label>&lt;?php echo form_dropdown('q5', $score_options, ''); ?&gt;</p>
              
                <p>&lt;?php echo form_submit('submit', 'Submit vote'); ?&gt;</p>                
            
            &lt;?php echo form_close(); ?&gt;
            &lt;?php echo validation_errors('<p class="error">', '</p>');?&gt;
#2

[eluser]CodeIgniteMe[/eluser]
You can use set_select() instead of set_value()

http://ellislab.com/codeigniter/user-gui...rreference

I know that set_select() doesn't look too much of what you need, I also had problems with that and had to make my own functions myself.
#3

[eluser]EmmyS[/eluser]
I don't think I can use set_select, because I'm not writing out the individual values of the select box, nor am I trying to provide a hard-coded default value. I need to reset the dropdowns to the values the user selected prior to running the form validation. I'm using the form_dropdown function, so how do I get the actual selected value to pass into the second param of set_select? According to the set_select documentation,

Quote:The first parameter must contain the name of the select menu, the second parameter must contain the value of each item


To clarify, creating a dropdown using the form helper looks like this:


Code:
<p><label for="q5">Question 5: </label>&lt;?php echo form_dropdown('q5', $score_options, ''); ?&gt;</p>

If you try to just use the field name as the second param of set_select, it throws an error.


Code:
<p><label for="q5">Question 5: </label>&lt;?php echo form_dropdown('q5', $score_options, set_select('q1',q5)); ?&gt;</p>

Quote:A PHP Error was encountered

Severity: Notice

Message: Use of undefined constant q1 - assumed 'q1'
#4

[eluser]CodeIgniteMe[/eluser]
Did you use the form_validation library or just the form helper? If it is the former, did you set rules for your dropdowns?
Because the form helpers don't do anything if form_validation is loaded, thus, executing its own helpers in its (form_validation) methods. If you did not set a rule for the field, it will ignore the post value in it. That's what I have experience before.
I'm now trying the same method you are doing, using set_value to restore the selected value, and its now working for me.

My controller
Code:
Class Some extends CI_Controller{
public function reload(){
        $this->load->library('form_validation');
        if(isset($_POST['submit'])){
            $val =& $this->form_validation; // just used $val to call the library (for coding convinience)
            $val->set_rules('drp1','Drop 1',''); //you can set rules or just leave it blank
            $val->set_rules('drp2','Drop 2','');
            $val->set_rules('drp3','Drop 3','');
            $val->set_rules('drp4','Drop 4','');
            if($val->run()){
                    
            }
        }
        $data['drp1'] = array('1'=>'asdf','2'=>'qwerty','3'=>'zxcvb');
        $this->load->view('drp_view',$data);
    }
}

In my view (drp_view.php)
Code:
&lt;?=form_open('some/reload');?&gt;
&lt;?=form_dropdown('drp1',$drp1,set_value('drp1'));?&gt;
&lt;?=form_dropdown('drp2',$drp1,set_value('drp2'));?&gt;
&lt;?=form_dropdown('drp3',$drp1,set_value('drp3'));?&gt;
&lt;?=form_dropdown('drp4',$drp1,set_value('drp4'));?&gt;
&lt;?=form_submit('submit','Submit');?&gt;
#5

[eluser]EmmyS[/eluser]
I was using both form_validation and the form helper. No, there are no validation rules for the dropdowns. We don't need to validate them; I just needed to make sure that if the text fields that DO have validation rules fail, the dropdowns retail their values so the user doesn't have to select them all again. I got it figured out:

Code:
&lt;?php echo form_dropdown('q1', $score_options, set_value('q1', $this->input->post('q1'))); ?&gt;
#6

[eluser]CodeIgniteMe[/eluser]
It's either one, or the other. Both methods will work. It just looks better when you don't include complex codes in your views. But, anyway, it works, so just go on with what you're doing.




Theme © iAndrew 2016 - Forum software by © MyBB