Welcome Guest, Not a member yet? Register   Sign In
Form_validation question
#1

Hi everybody, I'm developing a web application using this excellent framework (Codeigniter 3.0RC3)
Someone could help me whith this doubt.

I'm using form_validation library to validate data within models. I use the set_data method to validate an associative array. This is my validate method.

Code:
public function validate($data, $rules) {
        $this->form_validation->set_data($data);
        $this->form_validation->set_rules($rules);

        if ($this->form_validation->run() == FALSE) {
            return FALSE;
        }
        else {
            return $data;
        }
    }

Some validations rules are like this

Code:
$rules[] = array("field" => "description", "label"=> "Description", "rules"=> "trim|strip_tags");

After validation process takes place, $data["description"] is not trimmed and tags striped. The only way I can do this is re-populating $data array using set_value() function for each of the array items. For example

$data["description"] = set_value("description");

Thanks in advance guys.
Reply
#2

(This post was last modified: 03-17-2015, 12:25 AM by Avenirer. Edit Reason: additional info on set_value() )

As you can see, it's a validation library, not a cleaning one. Its purpose is only to validate the data.

set_value() on the other hand deals with output, so it's normal for it to clean what it receives.
Reply
#3

(This post was last modified: 03-17-2015, 03:55 AM by RobertSF.)

(03-17-2015, 12:19 AM)Avenirer Wrote: As you can see, it's a validation library, not a cleaning one. Its purpose is only to validate the data.

But even Codeigniter 2's validation library will clean data. This is from the manual.

Quote:Prepping Data
In addition to the validation functions like the ones we used above, you can also prep your data in
various ways. For example, you can set up rules like this:

Code:
$this->form_validation->set_rules('username', 'Username',
'trim|required|min_length[5]|max_length[12]|xss_clean');
$this->form_validation->set_rules('password', 'Password', 'trim|required|matches[passconf]|md5');
$this->form_validation->set_rules('passconf', 'Password Confirmation', 'trim|required');
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');

In the above example, we are "trimming" the fields, converting the password to MD5, and running the username through the "xss_clean" function, which removes malicious data. Any native PHP function that accepts one parameter can be used as a rule, like htmlspecialchars, trim, MD5, etc.

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.
Hey, don't work without a PHP debugger. Several free IDEs have this features built in. Two are NetBeans and CodeLobster. Without a debugger, it's like you're driving with a blindfold on -- you are going to crash!
Reply
#4

"Prepping" the data and cleaning the data are two different things. Of course you can use PHP functions that cleans the data inside the validation, but that is not the purpose of the validation library. Also, xss_clean is no longer part of form validation library but of security library. Blush
Reply
#5

Even though "prepping" and cleaning the data are two different things, you can do both inside validation in CI2. You can use any PHP function that has just one parameter in your validation rules. Did that change in CI3?
Hey, don't work without a PHP debugger. Several free IDEs have this features built in. Two are NetBeans and CodeLobster. Without a debugger, it's like you're driving with a blindfold on -- you are going to crash!
Reply
#6

CI3 still allows you to do just about anything with the data in the form validation library. It appears that the library places the updated data into $_POST, regardless of whether it is processing a supplied data array or the $_POST data. So, at least for now, you're stuck with repopulating your supplied array (via set_value()).
Reply




Theme © iAndrew 2016 - Forum software by © MyBB