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

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

[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')?>" >
#3

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

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

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

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

[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'));
#8

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

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

[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
#10

[eluser]Craig Ward[/eluser]
Cheers,

Have everything working now Smile




Theme © iAndrew 2016 - Forum software by © MyBB