CodeIgniter Forums
set_value() only working if the field is part of validation - CI 2.0 - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: set_value() only working if the field is part of validation - CI 2.0 (/thread-37344.html)

Pages: 1 2


set_value() only working if the field is part of validation - CI 2.0 - El Forum - 01-06-2011

[eluser]Craig Ward[/eluser]
Hi There,

I am using form validation in my CI project. When an error is found I want to reload the form, show the errors and repopulate the form. This work fine with fields that are part of the validation, but any fields that do not need validation do not get repopulated.

Is this a bug for CI 2.0?


set_value() only working if the field is part of validation - CI 2.0 - El Forum - 01-06-2011

[eluser]Atharva[/eluser]
This is not a bug and same behavior is for CI 1.7 . To re-populate those fields, simply use
Code:
<input type='text' name ='somename' value="<?=$this->input->post('somename')?>" >



set_value() only working if the field is part of validation - CI 2.0 - El Forum - 01-06-2011

[eluser]Craig Ward[/eluser]
Cheers for the reply. How come set_value() doesn't work as this is what is described in the user guide?

Also <?=$this->input->post('somename')?> works fine for inputs, but what about drop downs?


set_value() only working if the field is part of validation - CI 2.0 - El Forum - 01-06-2011

[eluser]Atharva[/eluser]
set_value() will not work for the fields which are not part of the validation rules. For dropdowns (or for any field), you can add the fields under validation rules like
Code:
$this->form_validation->set_rules('checkbox1', 'Checkbox1','trim|xss_clean');

So that it can be repopulated with set_select.


set_value() only working if the field is part of validation - CI 2.0 - El Forum - 01-06-2011

[eluser]Craig Ward[/eluser]
And if there not part of the validation process? Which helper should I look at for documentation?


set_value() only working if the field is part of validation - CI 2.0 - El Forum - 01-06-2011

[eluser]Atharva[/eluser]
I am afraid for that, you have to use
Code:
$this->input->post('name')
or you can set values for dropdowns using javascript.


set_value() only working if the field is part of validation - CI 2.0 - El Forum - 01-06-2011

[eluser]Phil Sturgeon[/eluser]
set_value() are for the form helper yes. $this->input->post() works the same as set_value() but does not need to be part of validation.

Code:
form_dropdown('name', array('foo', 'bar'), $this->input->post('name'));



set_value() only working if the field is part of validation - CI 2.0 - El Forum - 01-06-2011

[eluser]Craig Ward[/eluser]
Thanks for the response phil.

Out of interest how come there are two ways of repopulating a form?


set_value() only working if the field is part of validation - CI 2.0 - El Forum - 01-06-2011

[eluser]Phil Sturgeon[/eluser]
If you are repopulating a form its most likely you are using Form_validation. set_value() is a helper specific to that and $this->input->post() is just a generic helper for working with $_POST. You could say $_POST['name'] would be a third way of repopulating the form. :-P


set_value() only working if the field is part of validation - CI 2.0 - El Forum - 01-06-2011

[eluser]Craig Ward[/eluser]
Cheers,

Have everything working now Smile