[eluser]mradlmaier[/eluser]
Hello All,
Does anybody know if and how it is possible to combine the form helper functions form_dropdown() and set_select(), so that the users selection is remembered after form validation?
Michael
[eluser]Erwin Setiawan[/eluser]
In ci 1.7
I know that you want 
I used set_value() from form_validation library..
not set_select()
in controller:
Code: $options = array(
'small' => 'Small Shirt',
'med' => 'Medium Shirt',
'large' => 'Large Shirt',
'xlarge' => 'Extra Large Shirt',
);
then in view:
Code: form_dropdown('shirts', $options, 'large');
change 'larger' to set_value('shirts')
so your code will be:
Code: form_dropdown('shirts', $options, set_value('shirts'));
Don't forget to add validation rules on shirts dropdown.
Hope it useful for you
[eluser]mradlmaier[/eluser]
Hi,
I already run my own validation now. But I gave your proposal a try, because it sounds more elegant and involves less code. But unfortunateley it does not work (though I do not get an error message, just an empty page).
How do you setup a validation rule for the select element? Validating a select element sounds odd, but the first option displays "Please choose...", so I need to make sure that the first option is not selected...
As I mentioned already, I run my own validation scheme now, which works, but I would prefer to stick to the CodeIgniter validation, because it is more elegant, and less code...
Michael
[eluser]Erwin Setiawan[/eluser]
Did you add
Code: $this->form_validation->set_rules('shirts', 'Shirts', 'trim|required');
You must set rules to required for get back your value after submit.
[eluser]coffey[/eluser]
I don't think it is true that you must set an accompanying rule. I do this without and it works in an add/edit scenario
Code: $selectname = "myselect";
$options = array(
""=>'Choose',
"option1"=>"option1",
"option2"=>"option2",
"option3"=>"option3"
);
echo form_dropdown( $selectname, $options, set_value( $selectname, $$selectname ) );
Quick, easy and very helpful for large forms
[eluser]Future Webs[/eluser]
[quote author="coffey" date="1228071097"]I don't think it is true that you must set an accompanying rule. I do this without and it works in an add/edit scenario
Code: $selectname = "myselect";
$options = array(
""=>'Choose',
"option1"=>"option1",
"option2"=>"option2",
"option3"=>"option3"
);
echo form_dropdown( $selectname, $options, set_value( $selectname, $$selectname ) );
Quick, easy and very helpful for large forms[/quote]
does this also show your initial value during an edit though ?
[eluser]coffey[/eluser]
Yes, as long as you are sending the view a variable with the initial value .....
Code: $data['myselect']='valueFromDb';
$this->load->view('myview',$data);
also for radio boxes
Code: $radio="myradio";
echo form_radio($radio, 0, set_value($radio, ($$radio==0)?TRUE:FALSE));
and checkboxes - looks for the variable if its not there (or not =1) it is not checked.
Code: $box="Monday";
echo form_checkbox($box, "1", set_value($box, (isset($$box)?$$box:0)));
Note: $box .... $$box. '$$box' makes a variable from the value of '$box' as above $box='Monday' - $$box creates $Monday.
[eluser]Future Webs[/eluser]
Heres my example that I got working for a dropmenu
Code: <?php
$options = array(
'' => 'Select Experience',
'None' => 'None',
'Amature' => 'Amature',
'Experience' => 'Experienced',
'Professinal' => 'Professional'
);
echo form_dropdown('experience', $options, set_value('experience', (isset($my_data->experience)) ? $my_data->experience : '')) ;
echo form_error('experience') ;
?>
The thing that was causing a problem though and making me think this did not work was that if I did not have a validation rule set up for this field then it would not remember the choice if a mistake was made on the form etc
Is this how it should work ?
I would have thought that I would not have needed to set a blank validation rule just to get it to remember the selection. I thought it used the POST values
[eluser]coffey[/eluser]
It should work without validation ... /system/helpers/form_helper shows that it looks for the validation object and if it exists sends it there otherwise it just processes the $_POST value.
Works OK with me without any validation rules set .... prize pain in the derriere if it didn't especially with some of my large forms.
[eluser]Future Webs[/eluser]
would anything in my code shown above be causing the issue as i tried with and without the validation and it wouldn't work without
|