Codeigniter form validation to model logic
I'm working on my first CodeIgniter application and have encountered some confusion around handling post data.
More specifically, I understand the role of $this->form_validation->set_rules()
, and how set_value()
works to repopulate input values when the validation failed, but what I can't figure out is if $this->input->post
is the same value as the set_value
equivalent.
I understand that the majority of validation rules would have a boolean result, however what about ones like trim|htmlspecialchars
- these also have the benefit of preparing the data for db queries.
So in my model, can I access the inputs after being processed by the Form Validation library, or should I do the additional preparation inside the model directly onto $this->input->post('variable')
?
My gut tells me that I should add final processing like htmlspecialchars
right before the SQL in the model, as it is really a db specific operation (I would not want &
instead of &
in my form input开发者_Go百科s, but I would want &
in the database).
Incidentally in my reading I did come across $this->validation->variable
which would appear to have been the answer to my question in previous CI versions.
They are similar, but not exactly the same.
You would use $this->input->post('variable');
within the controller only. This variable will be validated and cleaned (if you decide to clean it with xss_clean
or apply any other prepping functions).
set_value()
should only be used within the Views. While this is not essential, the real value of doing this opposed to using $this->input->post
is you can set a "default" value as the 2nd parameter which is automatically used if the post value is empty.
As mentioned set_value()
is strictly for repopulating form inputs - nothing else. For example, set_checkbox()
is going to return something like checked="checked"
which is obviously not what you want to send to your model.
There's room for argument about where the input validation and prepping should be handled, but most will agree that it should be done in the controller.
You can do additional processing in the model if you wish, but generally you don't want to be accessing $_POST
from the model - it makes the model less useful. Not all data is going to be coming straight from the user, so it's better to prep the data beforehand and send it as a new array/object to the model. The model should not care where the data is coming from.
Let the form validation library and controller layer process the user input (what it's intended for), and the model can process the data you send to it.
Developer
I work both frame work cakephp and codeignator. I feel best thing cakephp data validation. spouse you have 8 fields form you create validation in controller with designator. After validation all fields is empty if you fill 8 fields and by mistake 1 fields miss then after validation codeignator data validation refresh page and empty all fields.
but in cake php just opposite data validation create in model you create one time validation and use some thins when you call model then call validation exp: add,edit.
精彩评论