Welcome Guest, Not a member yet? Register   Sign In
Data validation class
#1

[eluser]Unknown[/eluser]
I love Code Igniter but I never liked the fact that you cannot implement the Skinny Controller Fat Model architecture, mainly because you cannot impose data validation in Models (without having to refernce on POST variables).

That's why I have created the Data Validation class. It works in the same way as the form validation class but allows to validate any kind of data (even the parameters of model methods). You can place callbacks in the model or any object loaded in the CI factory class. Finally, you can map validated data to $_POST data and propagate error messages from the Data Validation class to the Form Validation class so that your views can still use all the Form Helper functions as (validation_errors(); etc.)

Using in Models is as simple as this:
Code:
function add_user($user_name,$user_surname)
  {
  $this->load->library('data_validation');
  //Feed our library with an array containing the values to be validated
  $this->data_validation->import_data_array(get_defined_vars());
  //get_defined_vars() will return the following
  //array('user_name'=>$user_name,'user_surname'=>$user_surname);

  //Specify where Data validator should try to find callbacks
  $this->data_validation->set_validator_object($this);

  $this->data_validation->set_rules('user_name','Name','trim|required|callback_name_surname_unique[user_surname]');
  $this->data_validation->set_rules('user_surname','Name','trim|required');
  if ($this->data_validation->run()==TRUE)
  {
   $this->db->insert('users',array('name'=>$name,'surname'=>$surname));
   return TRUE;
  }
  else
   return FALSE;
}
  function name_surname_unique($name,$surname) //Callback function
{
  //Get the value from the data validator data array
  $surname_value = $this->data_validation->_data_array[$surname];
  $ret = $this->db->from('users')->where('name'=>$name)->where('surname'=>$surname_value)->get("id");
  if ($ret->num_rows()==0)
   return TRUE;
  $this->data_validation->set_message('name_surname_unique','This user is already in the database');
  return FALSE;
}

In controller you just need to map the method parameters to post variables
Code:
if ($this->input->post('submit'))
    {
      $this->load->model('Users_model');
      if ($this->Users_model->add_user($this->input->post('name'),$this->input->post('surname')))
      {
        $this->load->view('success');
      }
      else
      {
        //First of all we need to tell the data validator how $_POST variables are mapped to the model methods
        //We create an array with all the fields following this pattern
        // array( "model_method_parameter_name"=>"post_variable_name");
        $post_map = array("user_name"=>"name","user_surname"=>"surname");        

        //Then we feed this array to the data validation object.
        $this->data_validation->map_to_post($post_map);

        //And we instruct the data validator to propagate error messages to the form validator
        $this->data_validation->propagate_to_form_validation();
      }
    }
    $this->load->view('add_form');

This way, you will not need to do any changes in your views.

For examples and complete explanation visit http://www.webrevised.com/148-code-ignit...ion-class/.

You can also download through the wiki (http://codeigniter.com/wiki/File:data_validator.zip)




Theme © iAndrew 2016 - Forum software by © MyBB