Re-populating form values on validation fail - dropdowns |
[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
[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.
[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><?php echo form_dropdown('q5', $score_options, ''); ?></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><?php echo form_dropdown('q5', $score_options, set_select('q1',q5)); ?></p> Quote:A PHP Error was encountered
[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{ In my view (drp_view.php) Code: <?=form_open('some/reload');?>
[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: <?php echo form_dropdown('q1', $score_options, set_value('q1', $this->input->post('q1'))); ?>
[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. |
Welcome Guest, Not a member yet? Register Sign In |