Mr. Carl Thu Jan 26 22:20:02 -0500 2012

Subject: Fetch validation rules out of Models

A way to fetch validation rules out of Models
How to fetch validation rules

In the next stable build can you setup way to use the Model::get_validation_rules() without the need to instantiate the User Model. I can understand way currently you must instantiate the Model first.

In the A way to fetch validation rules out of Models topic stated "open to tweaking the returned array format a bit".

Current get_validation_rules() return format:

1  array(
2    'name' => array(
3      array('validator' => 'validates_presence_of'),
4      array('validator' => 'validates_inclusion_of', 'in' => array('Bob','Joe','John')),
5 'password' => array(
6      array('validator' => 'validates_length_of', 'minimum' => 6))
7    )
8  );

To converting the output into something that bit more clean to deal with is bit dirty.

New format get_validation_rules() return format:

 1 array
 2 (
 3    'name' => array
 4    (
 5          'validates_presence_of' => true,
 6          'validates_inclusion_of' => array
 7          (
 8                    'in' => array('Bob','Joe','John')
 9          ),
10          'validates_inclusion_of' => array
11          (
12                    'in' => array('Bob','Joe','John')
13          )
14      ),
15    'password' => array
16    (
17          'validates_length_of' => array('minimum' => 6)
18    )
19  );

This allows for cleaner looking code in

 1 
 2     // Used for getting validation and save data
 3     $user = new user();
 4 
 5     // Get all the validation from the user model
 6     $model_validation = $user->get_validation_rules();
 7 
 8     // CodeIgniter form class
 9      $this->form_validation->set_rules('password', 'Password', 'required|min_length['
10         .$model_validation['password']['validates_length_of']['minimum']
11         .']');
12