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

Hi guys,

I was just wondering what is the best way to have a dynamic form validation. Let's say you don't know what the _POST values are, well they could be anything, but you set a rule in the database as to what validation rule it needs.

Is it possible to generate a dynamic form validation on the fly and then repopulate the view if said input fails using set_value().

And what would be best to store the validation rules. Maybe a json array?

Regards,

Iamthwee.
Practical guide to IgnitedCMS - Book coming soon, www.ignitedcms.com
Reply
#2

Any tips would be useful? Big Grin
Practical guide to IgnitedCMS - Book coming soon, www.ignitedcms.com
Reply
#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
#4

(This post was last modified: 10-17-2015, 02:00 AM by ignitedcms.)

Thanks for the tips mwhitney.

I've opted for saving each field in the database with a field type for example, plain text, rich text, number etc and save the form validation in a column in the database.

For example, plain text => alpha|max_length[10]

Then I loop through the database and dynamically build the form validation using a validation array.

The only specific cases are images or assets, which basically are pulled from another table specific for these,which contains the information such as asset type, image height/width and url.

For dropdown boxes and checkboxes I am saving the details inside a json array.

{ "opt1":"hello", "opt2":"goodby"}

I'm going to implement the json array and assets tomorrow. Everything is done.
Practical guide to IgnitedCMS - Book coming soon, www.ignitedcms.com
Reply




Theme © iAndrew 2016 - Forum software by © MyBB