CodeIgniter Forums
Validation 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: Validation question (/thread-2509.html)



Validation question - El Forum - 08-09-2007

[eluser]chobo[/eluser]
I was doing some validation with the validation class and I was just wondering where do the variables that you are testing get stored? Currently, I validate the variables and then retrieve them from the post array like this:

Code:
$this->input->post('somefield', true);

Does retrieving the variables using CI's input->post class get the validated variables, or unfiltered ones directly from the form?

Thanks!


Validation question - El Forum - 08-09-2007

[eluser]Michael Wales[/eluser]
The validation class doesn't actually change or do anything to your variables.

The validation class checks your variables against your rules - and returns whether they pass or fail via the run() mthod.

So, if you have a validation setup and going, you would use the following code:
Code:
if ($this->validation->run()) {
  // All my variables passed the rules I established
} else {
  // My variables did not pass
}

Yes, you would access your variables via the input->post() method just as you would without the validation class.


Validation question - El Forum - 08-09-2007

[eluser]chobo[/eluser]
In the documentation it says you can use functions to prepare data when you set the validation rules like: trim, htmlspecialchar, etc... I wasn't sure if I could somehow access those values because if I prepare the values in the validation rules and then after call post to retrieve them I don't see how those values will still be prepared.

Maybe an example will explain this better.

The following does a validation check and preps the data using htmlspecialchars
Code:
// validate and prep data
$rules['name'] = "required|htmlspecialchar";

If I then call post on the field to retrieve it
Code:
$this->input->post('name');

Will it have the htmlspecialchars applied to it? If not, how would I get the version that does? I'm a little confused by the documentation as it uses the word "prep", so I'm assuming I can access the prepped version of the data.


Validation question - El Forum - 08-09-2007

[eluser]Michael Wales[/eluser]
You know - I actually had to stop and think about this one. I thought the validation class did correct the post variables, but I wanted to make sure. If you run the following code and enter ' test ' into the field - it will print the string from the view without any of the leading spaces, therefore - the validation class does in fact apply it's functions to the post variables.

For a second there I thought you would have to access them via the validation class. I've always accessed it via the input class and wanted to make sure what I thought to be true, was in fact true.

Controller: test.php
Code:
<?php

class Test extends Controller {

  function index() {
    $this->load->library('validation');
    $rules['string'] = 'trim|required';
    $fields['string'] = 'string';
    $this->validation->set_rules($rules);
    $this->validation->set_fields($fields);
    
    if ($this->validation->run()) {
      // Validation passed
      $data['stuff'] = 'this is' . $this->input->post('string') . 'a test';
      $this->load->view('test', $data);
    } else {
      $data['stuff'] = '';
      $this->load->view('test', $data);
    }
  }

}

?>

View: test.php
Code:
String Result: <?= $stuff; ?>
<?= form_open('test'); ?>
String: &lt;input type="text" name="string" id="string" /&gt;<br />
&lt;input type="submit" value="Submit" /&gt;
&lt;?= form_close(); ?&gt;



Validation question - El Forum - 08-09-2007

[eluser]chobo[/eluser]
Thanks a lot for testing it! Ya, I've been using the validation class for awhile and it just crossed my mind today. It's nice to know that it actually updates the post variables Smile