Welcome Guest, Not a member yet? Register   Sign In
set_value() only working if the field is part of validation - CI 2.0

[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?

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

[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() 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
$this->form_validation->set_rules('checkbox1', 'Checkbox1','trim|xss_clean');

So that it can be repopulated with set_select.

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

I am afraid for that, you have to use
or you can set values for dropdowns using javascript.

[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.

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

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

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

[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

[eluser]Craig Ward[/eluser]

Have everything working now Smile

Theme © iAndrew 2016 - Forum software by © MyBB