Welcome Guest, Not a member yet? Register   Sign In
Form Validation Default
#7

First, I wanted to draw attention to the is_unique rule built into the form validation library. This will tell you if the value of the field already exists in the table/column (requires Query Builder and can only really be used on insert, since the value will exist when you attempt to update the data): http://www.codeigniter.com/user_guide/li...-reference

Second, you can extend the form_validation library by creating a MY_Form_validation class which extends CI_Form_validation. This allows you to add public methods to the validation library which can be used as validation rules anywhere else you happen to be using the form validation library. I use this for rules which are generally handy throughout my site, but not tied to any particular model/controller. Bonfire has a handful of rules defined this way, and I add some of my own in my projects.

Finally, for model-specific validation, I usually store my validation rules in the model, but I perform validation in both the model and the controller. Sometimes I have the controller retrieve the validation rules from the model and apply them to the form, but there may be instances where the controller is dealing with data from multiple models. Validation performed by my controller is specific to the action being taken. Validation performed by my model is specific to making sure my data is safe to put into the database. It's quite rare that I find a reason not to perform data validation on the form, but it does occasionally happen, and in those cases I don't return those specific rules to the controller, but still check them in the model before inserting/updating/deleting data.

If you're a little more paranoid in your model's acceptance of data, you're far less likely to run into issues later if you're using a model in multiple places.
Reply


Messages In This Thread
Form Validation Default - by edoramedia - 05-22-2016, 12:32 AM
RE: Form Validation Default - by skunkbad - 05-22-2016, 12:40 AM
RE: Form Validation Default - by cartalot - 05-22-2016, 12:06 PM
RE: Form Validation Default - by albertleao - 05-22-2016, 05:23 PM
RE: Form Validation Default - by cartalot - 05-26-2016, 09:49 AM
RE: Form Validation Default - by albertleao - 05-26-2016, 04:29 PM
RE: Form Validation Default - by skunkbad - 05-26-2016, 10:48 AM
RE: Form Validation Default - by mwhitney - 05-26-2016, 02:14 PM
RE: Form Validation Default - by PaulD - 05-26-2016, 09:05 PM



Theme © iAndrew 2016 - Forum software by © MyBB