Welcome Guest, Not a member yet? Register   Sign In
dynamic form validation
#3

Bonfire has quite a bit of validation-related code in its model, and some of that code came from a model I developed specifically for prototyping. One of the functions specific to prototyping is an observer that is triggered when the get_validation_rules() method is called but the model does not have any validation rules. In my prototype model, when this observer is triggered, I get the field info, which can be stored in the model or retrieved from the database:

PHP Code:
   public function get_field_info()
 
   {
 
       if (empty($this->field_info)) {
 
           $this->field_info $this->db->field_data($this->table_name);
 
       }

 
       return $this->field_info;
 
   

Then I loop through the fields and build a validation array based on the data types of the fields (int/tinyint/bigint/etc. use the integer validation rule, decimal/double/etc. use the numeric rule), and, if the field info includes a max_length for the field, I add that to the validation as well.

It's very crude, but it allows me to demonstrate the way validation works in a prototype. Since the observer only fires if the validation rules aren't defined for the model, it also allows me to define the validation array and not have to worry about my auto-generated rules.

It would be relatively simple, though, to modify the idea to pull the validation rules from any number of locations, like a config file or even a table in the database. However, if I were going to pull validation rules from the database, I'd probably want to keep it simple and only support a string for the rules, rather than trying to support everything supported by the form_validation library. The validation array needs a field, label, and rule, so it would be pretty straightforward to store three strings to build that array for a given field.
Reply


Messages In This Thread
dynamic form validation - by ignitedcms - 10-14-2015, 03:12 PM
RE: dynamic form validation - by ignitedcms - 10-15-2015, 11:52 AM
RE: dynamic form validation - by mwhitney - 10-16-2015, 08:26 AM
RE: dynamic form validation - by ignitedcms - 10-17-2015, 01:59 AM



Theme © iAndrew 2016 - Forum software by © MyBB