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:
<?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><?php echo form_input($entry_code_data); ?></p>
<p><label for="q1">Question 1: </label><?php echo form_dropdown('q1', $score_options, set_value('q1')); ?></p>
<p><label for="q2">Question 2: </label><?php echo form_dropdown('q2', $score_options, ''); ?></p>
<p><label for="q3">Question 3: </label><?php echo form_dropdown('q3', $score_options, ''); ?></p>
<p><label for="q4">Question 4: </label><?php echo form_dropdown('q4', $score_options, ''); ?></p>
<p><label for="q5">Question 5: </label><?php echo form_dropdown('q5', $score_options, ''); ?></p>
<p><?php echo form_submit('submit', 'Submit vote'); ?></p>
<?php echo form_close(); ?>
<?php echo validation_errors('<p class="error">', '</p>');?>
I've had someone on the CodeIgniter forum tell me to use set_select, but 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,
The first parameter must contain the name of the select menu, the second parameter must contain the value of each item
There was also a comment on this post that said to use set_value -echo form_dropdown('events', $options, set_value('events'), $firstItem);
. The comment got three votes, so I'm assuming that it works for some people, but doesn't do anything for me. That question was a year old; perhaps that worked in an older version of CI but not the newest one. In any case, there HAS to be a way to have "sticky" dropdown values when creating dropdowns using form_dropdown, doesn't there? I can't imagine that I'd have to do it the long way and create the dropdown by looping through an array of values and echoing out each option line separately; that kind of defeats the purpose of using something like CI in the first place.