CodeIgniter Forums
set_value | set_select | etc. works only with rule applied on its element - 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 | set_select | etc. works only with rule applied on its element (/thread-31191.html)



set_value | set_select | etc. works only with rule applied on its element - El Forum - 06-09-2010

[eluser]Jay Gridley[/eluser]
Hi foks,

I have form for creating new page in my CMS. I am validating some fields with form_validation library. I have several input boxes in my form. Few of them are validated using rules, BUT other input boxes are not.

All these input boxes has value set up with set_value(), BUT only boxes, which are validated have previosly inputed value filled in, when error during validation came up. Others NOT! Am I getting something wrong or it is bug?

Example:
Code:
$urlInput = array(
        'name' => 'url',
        'id' => 'url',
        'value' => set_value('url'),
        'maxlength' => '255',
        'size' => '50'
    );
    $pageInfo .= form_label('URL','url'); [color=red]//THIS INPUT BOX IS VALIDATED[/color]
    $pageInfo .= form_input($urlInput);

$timeInput = array(
        'name' => 'time',
        'id' => 'time',
        'value' => set_value('time'),
        'maxlength' => '8'
    );
    $pageInfo .= form_label('Time','time'); [color=red//THIS ONE NOT[/color]
    $pageInfo .= form_input($timeInput);
When there is an error during the validation process (some other box should be filled, but it is empty), so I am loading view with form again (with error message) and input box for URL has value, which I entered last time (before validation). I also entered value into time input box (before validation), but after validation, my value for this input box is lost.

I figgured out, if I setup rule for time input box, after validation it is filled with previosly entered value. So, this is my point. Did you get it? Same problem with other methods for checkboxes or selectboxes.


set_value | set_select | etc. works only with rule applied on its element - El Forum - 06-09-2010

[eluser]mddd[/eluser]
If I remember correctly, set_value takes its information from the validation library. And the validation library only works on the fields it knows about (that is: fields that have some kind of rules attached to them).

So the solution is:
1. put all fields in your validation configuration, even if the don't need rules to check them.
or
2. manually set the right value with set_value('fieldname', $my_value);
Here, $my_value allows you to set your own value if there is no value set for 'fieldname' trough the validation library. You could set $my_value to $this->input->post('fieldname') to pick up the posted information, even if it is not validated: set_value('fieldname', $this->input->post('fieldname');

BUT: generally, it is a good idea to validate any fields coming from the user. Even if you're not applying rules but just checking them for xss injection.


set_value | set_select | etc. works only with rule applied on its element - El Forum - 06-09-2010

[eluser]Jay Gridley[/eluser]
You´re right! Thanks a lot. Save me a good headache.