[eluser]McNoggin[/eluser]
I have only been using CI for a month or 2 so I'm still pretty new at it. I was working on some forms today and realized I was writing more or less the same code over and over.
My controllers almost always do the following:
1. setup the validation rules
2. run the validation
3. build an array from the post data
4. pass array it into a model ($this->db->insert( 'tableName', $valPostData ), etc)
I'd like to know if there is a way to skip step 3, or at least make it a single call instead of one line for each field. The form_validator already knows all of the fields I care about because I created rules for them in step 1. I'm not going to insert any data the user input into my database that hasn't been ran through the validator.
My thinking (assuming it doesn't already exist and I just don't know about it) is to extend the form_validator and add a function that does this. Not sure of the variable names but more or less it would do the following:
Code:
function getValidPostData()
{
foreach($validation_rule as $rule)
{
$validPostData[ $rule['field'] ] = $this->input->post( $rule['field'] );
}
return $validPostData;
}
That way I could take the array that returns and pass it right into my CRUD model. Has anyone else done something similar? Or are there any reasons not to do this (security or other)?