tags:

views:

1671

answers:

3

Which is right? notice in the second option, I'm passing the form values using the $_POST variable. Whereas the first option, I call and assign variables for each form field.

I've seen this ...

<validation code> ....

$todo = array(
      'name'=>$this->input->post('title'),
      'description'=>$this->input->post('description')
);

$this->Todo_model->add($todo); 

But I've also seen the following ...

$records['email']    = "trim|required|min_length[4]|xss_clean";
...
...    

$this->validation->set_rules($records);

if ($this->validation->run())
{
   $this->account_model->saveAccountSettings("sam", $_POST);
   $this->session->set_flashdata('message', 'Done!');            

   redirect('account/settings');
} else {
...
} 
A: 

The first option is better easy to read or trace Pass values using post variables is better option

Gerard Banasig
A: 

I tend to use a mix of your two examples. I'm pretty sure things like trim won't modify the actual post data, so you can only take advantage of it if you go through the validation framework to get the data. I actually never access POST directly anymore using CI.

Plus I'd be worried in your second example about just shoving POST into my model. What happens if someone clever adds "lastname" to the post data sent in and your db column is named the same? Even though you weren't expecting to deal with that data now you've got unvalidated data coming in. That's why I employ part of your first example and manually pull out the items I want to save into an array first.

So I'd recommend a hybrid.

Normally my code looks something like this:

$fields['email']    = "trim|required|valid_email|min_length[4]|xss_clean";
...
...    

$this->validation->set_rules($fields);

if ($this->validation->run())
{
   $account = new array();
   $account['id'] = $accountId; //wherever you get the Id from
   $account['email'] = $this->validation->email;

   $this->account_model->save($account);
   $this->session->set_flashdata('message', 'Done!');            

   redirect('account/settings');
} else {
...
} 
Parrots
Not understanding this line here.$account['email'] = $this->validation->email;What is it doing? I mean I see it's validating the field, is that where the first field comes into play?
luckytaxi
You can use $this->validation->something like you'd use $this->input->post('something'). It'll get you the value from POST after it's been run through the validation framework, taking into account things like trim and XSS.
Parrots
Does the "something" have to be define on the first line? See where it says $fields['email'] ... like if I only set up "validation rules" for email, I can do $this->validation->id, yes?
luckytaxi
You'll have access to $this->validation->something for every rule you define (in the above example $fields). $this->validation->id would only work, in this example, if you had a validation rule for ID ($fields['id'] = 'trim'; or somesuch) and the ID was passed back through the form.Basically for every field in the form I have a rule, and then every form field can be accessed through this->validation->something. This makes sure everything I get back is validated and filtered as needed by the rules.
Parrots
awesome! i run through so many tutorials and each one has something different. i like your explanation better. thanks so much!
luckytaxi
A: 

What the real benefit to use this

$account['email'] = $this->validation->email;

Instead of

$account['email'] = $this->input->post('email');

Mayank