[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: <input type="text" name="string" id="string" /><br />
<input type="submit" value="Submit" />
<?= form_close(); ?>