Welcome Guest, Not a member yet? Register   Sign In
Form Validation - Trim Twice for database insertion?
#1

[eluser]ScottBruin[/eluser]
Sorry if this is obvious, but I cannot figure out an easy way to do this. I have a bunch of post data I'm validating with CI's validation library. That works great. However, there are two main problems I've found. For the field 'AthleteNameFirst' I am validating by trimming it first, then using a max_length. Even though I've done this, it seems I need to set_fields in order to have the object $this->validation->AthleteNameFirst. Why? How come this doesn't work without this. I have no interest in setting field names for each field.

After I validate, I have the option of a) setting the field names and using $this->validation->AthleteNamefirst, or b) getting the data from the $_POST array with $_POST['AthleteNameFirst'] or $this->input->post('AthleteNameFirst').

I've attempted both (a) and (b) and both return the field value pre-trim, meaning if I use trim to validate, methods a and b both return an untrimmed string.

Why is this? Am I missing something, or is this how things are "supposed" to be? If you have any suggestions for a better workflow, please, let me know.

Thanks,

Scott
#2

[eluser]JayTee[/eluser]
One of the things I do with validation is set_rules and set_fields FIRST - before I try to validate:
Code:
<?php
function dostuff() {
  $this->load->library('validation');
  $fields = array(
    'AthleteNameFirst' => 'Athlete First Name',
    'AthleteNameLast' => 'Athlete Last Name'
  );
  $this->validation->set_fields($fields);
  $rules = array(
    'AthleteNameFirst'=>'trim|max_length[8]',
    'AthleteNameLast' =>'trim|max_length[60]'
  );
  $this->validation->set_rules($rules);
  if ($this->validation->run()) {
    //passed validation, do something
  } else {
    //did not pass validation, show errors
  }
}
?>
The set_fields and set_rules methods serve a couple of purposes:
- set_fields will ensure that the validation class stores the value of that field if you need it to be displayed later
- the names given to the set_fields array will be used to display error messages for the field name
- set_rules will handle the validation checks. In my example the fields don't have 'required', so the validator will only check those values if the user types something in.

If you're looking to trim the value - it all depends on where/when you want to trim it. I'll need some detail to help with that one.
#3

[eluser]BravoAlpha[/eluser]
[quote author="ScottBruin" date="1190088124"]I have no interest in setting field names for each field.[/quote]

I use something like this (with a language file):
Code:
// Field names for error messages
foreach(array_keys($rules) as $key)
    $fields[$key] = $this->lang->line('field_'.$key);

This should also work:
Code:
// Field names for error messages
foreach(array_keys($rules) as $key)
    $fields[$key] = $key;
#4

[eluser]ScottBruin[/eluser]
Thanks for the quick responses, guys. Both ways you define field names (with a foreach loop or an array) will speed me up from where I'm at.

However, there's still one thing I remain unclear about. Why does CodeIgniter do certain things during validation that you must run again before db entry? For example, if you use 'trim' and 'htmlspecialchars' to prep for validation, how come you need to run these again to prep for data insertion? It seems repetitive to prep for validation and then do the same prep for DB insertion. In my OP I was looking for something that I may be missing that lets you access this already prepped string. Instead I'm finding I need to do things 4 times. Assign rule by name, prep for validation, assign field name (even if I don't want to) just to access object, and then prep for entry.

Ideally, I could run my validation rules and then insert this data into the database. And if I cannot do things like this, why are things like xss_clean available to validation if you'll need to run them again anyways?

//EDIT:

And in light of all this, what's the difference really between using $this->validation->AthleteNameFirst and $this->input->post('AthleteNameFirst') after validation? Sorry if I'm missing something obvious.
#5

[eluser]Kopel[/eluser]
Oh man, i keep asking myself exactly the same questions...
I'm pretty new to CI and i'm studying the manual to learn the best way to use the framework features.
But i'm still confused about the repeated works needed on the validation / database parts.

Any clarification will be appreciated.

Thanks!

EDIT
Well, i've done some tests.
Apparently the validator class acts directly on the $_POST array.
So, correct me if i'm wrong, we don't need to prep again for DB insertion...
#6

[eluser]Rick Jolly[/eluser]
[quote author="ScottBruin" date="1190157567"]
Why does CodeIgniter do certain things during validation that you must run again before db entry? For example, if you use 'trim' and 'htmlspecialchars' to prep for validation, how come you need to run these again to prep for data insertion?...
[/quote]
If you are truely needing to re-prep your data after validation, then perhaps you are using the validation class incorrectly. The validation class works directly on the $_POST array. So, for instance, if you use the trim prep for a field, then that field will be trimmed in the post array as well as in the validation object.
[quote author="ScottBruin" date="1190157567"]
//EDIT:
And in light of all this, what's the difference really between using $this->validation->AthleteNameFirst and $this->input->post('AthleteNameFirst') after validation? Sorry if I'm missing something obvious.[/quote]
They will be exactly the same, as will $_POST['AthleteNameFirst'].
#7

[eluser]esra[/eluser]
You might find this post interesting.

http://ellislab.com/forums/viewthread/51314/

Deeper in the thread is a post by a user with links to multiple Validation library extensions contributed in different threads or on the wiki.




Theme © iAndrew 2016 - Forum software by © MyBB