CodeIgniter Forums
Cheap Form Validation Trick - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Cheap Form Validation Trick (/showthread.php?tid=17489)



Cheap Form Validation Trick - El Forum - 04-06-2009

[eluser]cwt137[/eluser]
When you do form validation, and an error occurs, when the form gets re-populated, the fields without form validation rules do not get re-populated. This bug is very annoying. I found a cheap way around this problem until this problem is fixed. My solution is for the fields that don't require validation, still make a rule for it like so:

Code:
$this->form_validation->set_rules('foo_field','', 'callback__not_required');

Then in the call back function, just return true:

Code:
function _not_required($str) {
  return TRUE;
}

This way if some other field does not pass validation, the field that I don't need validated will still get re-populated.


Cheap Form Validation Trick - El Forum - 04-06-2009

[eluser]Thorpe Obazee[/eluser]
or you can use 'trim' on all your form inputs.


Cheap Form Validation Trick - El Forum - 04-07-2009

[eluser]Phil Sturgeon[/eluser]
Or even an empty string for the rule.


Cheap Form Validation Trick - El Forum - 04-07-2009

[eluser]cwt137[/eluser]
Thanks. I think I'll use the empty string for the rule.


Cheap Form Validation Trick - El Forum - 04-07-2009

[eluser]Colin Williams[/eluser]
I don't think it's a bug, but it's kind of a weakness brought on by the limited scope of the Form Validation class. It also kind of depends on how you go about repopulating your form fields.


Cheap Form Validation Trick - El Forum - 04-08-2009

[eluser]Thorpe Obazee[/eluser]
[quote author="Phil Sturgeon" date="1239123775"]Or even an empty string for the rule.[/quote]
lol. forgot about that :p