Welcome Guest, Not a member yet? Register   Sign In
set_value | set_select | etc. works only with rule applied on its element

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

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

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

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

Theme © iAndrew 2016 - Forum software by © MyBB