How Codeigniter’s set_value() of form validation works

Ok, I just found this one out.

set_value() of form validation does not work if you do not place a corresponding form validation rules using the $this->form_validation->set_rules();

To be able to retain a form’s field value even if it’s not required, you need to set rules for it.

For me, i place a “trim” rule. in this way set_value will work and retain the value of a field in case a validation rule is not met.




in the controller:


$this->form_validation->set_rules('misc', 'Notes','trim');

In the form:


<input type="text" name="misc" value="<?=set_value('misc');?>"/>


Otherwise, if you have not placed a set_rule to misc, the value of the field misc will not be retained in case a validation rule is not met.

Leave a comment

7 Comments.

  1. great post!!! exactly what I needed. No one else seemed to know this trick. Big up! :mrgreen:

  2. Cheers for the information.

    Just tried this and found out passing the value null doesn’t work, but you can use an empty string instead of the trim validation.

  3. I can confirm the empty string does work. Here is what I use to make sure all fields can be repopulated…

    foreach ($_POST as $key => $value) $this->form_validation->set_rules($key, ”, ‘trim’);

    Then further down I set the actual validation rules for individual fields :smile:

  4. whoops, that was a modified version :shock:

    foreach ($_POST as $key => $value) $this->form_validation->set_rules($key, ”, ”);

    That’s what I get for pasting without looking :roll:

Leave a Reply


[ Ctrl + Enter ]

Netfirms Business Hosting Plan
Blog WebMastered by All in One Webmaster.
Plugin from the creators of Brindes Personalizados :: More at Plulz Wordpress Plugins