Welcome Guest, Not a member yet? Register   Sign In
Form Validation: One set of rules for all forms of same model

(This post was last modified: 12-19-2015, 03:34 PM by gastongr.)

Hello everyone, I am using the CI form validation library in my controllers but i have the validation rules defined in the models.
For example in my Users controller, in the edit() method i have:

PHP Code:
// Get the rules from the model
$rules $this->user_model->user_edit_rules;

// Set the rules

if (
$this->form_validation->run() == TRUE) {
   // Edit user

Then in the same controller the other methods use different validation rules, for example user_login_rules, user_create_rules and so. This results in many sets of rules for the same fields.

The thing is that i want a single set of validation rules that represent the restrinctions of the whole model. For example:

PHP Code:
public $user_rules = array(
   'username' => array(
           'field' => 'username',
           'label' => 'User',
           'rules' => 'trim|required|min_length[4]|max_length[20]|is_unique[users.username]',
   'email' => array(
           'field' => 'email',
           'label' => 'Email',
           'rules' => 'trim|required|is_unique[users.email]|valid_email',
   'permissions[]' => array(
           'field' => 'permissions[]',
           'label' => 'Permissions',
           'rules' => 'trim|required',
   'password' => array(
           'field' => 'password',
           'label' => 'Password',
           'rules' => 'trim|required|min_length[5]|max_length[25]',
   'password_confirm' => array(
           'field' => 'password_confirm',
           'label' => 'Confirm Password',
           'rules' => 'trim|required|matches[password]',

But when i use this rules, for example, for the login page the validation fails because some data is not submitted in the login form.
So i want the validation library to only take into account the fields that have been submitted.

I think this would be possible since submitting an empty field means it will exist in $_POST[] as an empty string which is different from not submitting it.

Any advice on how to accomplish this? maybe implementing a MY_Form_validation.php but i am not sure.

Thanks so much Smile

use unset function before passing the array to set rule methode.
For example if yo dont want to validate password confirm in login page,

// Get the rules from the model
$rules = $this->user_model->user_edit_rules;


// Set the rules
          Heart  love codeigniter Heart
          Learning  best  practices
     Rate my post if you found it helpfull

Great thanks!
Your idea made me think i could do it like this:

PHP Code:
$login_rules elements(array('username''password'), $this->user_model->all_rules); 

Thanks again for taking the time to answer!

You might want to consider extending the Form validation class with a MY_Form_validation.php and putting the following method in it:

PHP Code:
 * Generic callback used to call callback methods for form validation.
 * @param  string  the value to be validated
 * @param  string  a comma separated string that contains the model name, 
 *                 method name and any optional values to send to the method 
 *                 as a single parameter.
 *                 - First value is the external class type (library,model)
 *                 - Second value is the name of the class.
 *                 - Third value is the name of the method.
 *                 - Any additional values are values to be 
 *                   sent in an array to the method. 
 *  external_callbacks[model,model_name,some_method,some_string,another_string]
 *  external_callbacks[library,library_name,some_method,some_string,another_string]
public function external_callbacks$postdata$param )
    $param_values explode','$param ); 

    // Load the model or library holding the callback
    $class_type $param_values[0];
    $class_name $param_values[1];
    $this->CI->load->$class_type$class_name );

    // Apply method name to variable for easy usage
    $method $param_values[2];

    // Check to see if there are any additional values to send as an array
    ifcount$param_values ) > )
        // Remove the first three elements in the param_values array
        $argument array_slice$param_values);

    // Do the actual validation in the external callback
    if( isset( $argument ) )
        $callback_result $this->CI->$class_name->$method$postdata$argument );
        $callback_result $this->CI->$class_name->$method$postdata );

    return $callback_result;

// ----------------------------------------------------------------------- 

This allows you to put commonly used form validation rules in a model or library. See the comments.

PS. This is included in Community Auth

Thanks for the suggestion @skunkbad !

Theme © iAndrew 2016 - Forum software by © MyBB