CodeIgniter Forums
Form validation rule question - 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: Form validation rule question (/thread-53472.html)



Form validation rule question - El Forum - 07-25-2012

[eluser]mrh[/eluser]
Hello all...

After searching the forums I figured out from a prior post that for set_value to work you have to define a rule for it in validation rules. So I've done that. For each field I've defined a rule like:

Code:
array (
  'field' => 'directorEmail',
  'label' => '',
  'rules' => 'trim'
  );

My questions are:

a) Do rules in the $config array run in order so that these "trim" rules would run BEFORE any custom rules I create at the end of the array?

b) Do these "trim" rules replace the post value? So I don't have to trim the data later? IE running these rules means I don't need to do trimming in my custom validation functions like this:

Code:
$_POST['directorEmail'] = trim( $_POST['directorEmail'];



Form validation rule question - El Forum - 07-25-2012

[eluser]CroNiX[/eluser]
Yes, rules are processed left to right.
Yes, rules like "trim", otherwise known as a "prepping function" do alter the values as they manipulate the data and return it. If a function returns a value other than a boolean TRUE/FALSE, that value will be the new value and the rule is assumed to "pass". If you return a TRUE, the rule passes, return FALSE and it doesn't.

Please read the [urlhttp://ellislab.com/codeigniter/user-guide/libraries/form_validation.html#preppingdata]Prepping Data section[/url] of the Form Validation library for more info.
Quote:Note: You will generally want to use the prepping functions after the validation rules so if there is an error, the original data will be shown in the form.



Form validation rule question - El Forum - 07-25-2012

[eluser]mrh[/eluser]
[quote author="CroNiX" date="1343263712"]Yes, rules are processed left to right.
Yes, rules like "trim", otherwise known as a "prepping function" do alter the values as they manipulate the data and return it. If a function returns a value other than a boolean TRUE/FALSE, that value will be the new value and the rule is assumed to "pass". If you return a TRUE, the rule passes, return FALSE and it doesn't.

Please read the [urlhttp://ellislab.com/codeigniter/user-guide/libraries/form_validation.html#preppingdata]Prepping Data section[/url] of the Form Validation library for more info.
Quote:Note: You will generally want to use the prepping functions after the validation rules so if there is an error, the original data will be shown in the form.
[/quote]

Thanks for that! What is not clear in the prepping section which I did read is when the form validation calls a prepping function like trim, is where is the result of trim stored. This is not clear form the prepping function section. If I read your reply correctly and the prepping section it sounds like a rule like I specified "trim" effectively does:

$_POST['field'] = trim( $_POST['field']

So that if I later get data out of $_POST['field'] that it has been trimmed right?


Form validation rule question - El Forum - 07-25-2012

[eluser]CroNiX[/eluser]
$this->input->post('field')
or
set_value('field')
if using form validation

You shouldn't use $_POST directly in CI.




Form validation rule question - El Forum - 07-25-2012

[eluser]mrh[/eluser]
Thanks! Another good piece of info.