Welcome Guest, Not a member yet? Register   Sign In
Model or Controller - Where to Validate
#1

[eluser]louis w[/eluser]
Previously I was skipping the Model layer when making small sites. Preparing for a larger application and would like to utilize the MVC pattern a bit more.

I am wondering in what layer do you validate the data? Do you do it before you pass your data to the Model (in the Controller) or before you do your database operation (in the Model)?

Wondering if the Model contains anything besides very bare bone database interaction.
#2

[eluser]Seppo[/eluser]
I prefer doing it in the controller... some validation rules for the same model may change depending on the triggered controller
#3

[eluser]wiredesignz[/eluser]
Validation is actually done in the Validation library, You can load the Validation in the Controller and run it from the Model.
#4

[eluser]louis w[/eluser]
Why don't you load it in the Model, right before you use it?

[quote author="wiredesignz" date="1208143921"]Validation is actually done in the Validation library, You can load the Validation in the Controller and run it from the Model.[/quote]
#5

[eluser]Michael Ekoka[/eluser]
The problem with the validation class is that it looks for custom validation rules inside the controller (callbacks). So whether you load it from the model or from the controller, you still will need to define these custom rules in the controller.

If you have 2 or more controllers handling the same data (the same model), they will each need to implement any of the custom validation rules (either copy/paste or inheritance from a base class that implements these rules).
#6

[eluser]wiredesignz[/eluser]
Or use my Validation callbacks into models extension.

@louis: Even if you load validation in a Model, it is actually attached to the controller so it makes no difference.
#7

[eluser]xwero[/eluser]
The best way in my opinion is between the controller and the model. The controller and the model shouldn't know what are the names of the form field nor of the database table fields. This makes it possible to share controllers and models for different applications.

I have a base class like this
Code:
class Tomodel
{
    var _ci;

    function Tomodel()
    {
        $this->_ci=& get_instance();
        $this->_ci->load('validation');
    }

}

The usage is
Code:
// controller
function useradd()
{
   $data['msg'] = $this->Tousers->useradd();
   $this->_ci->load->view('useradd',$data);
}
// Touser class
include('Tomodel.php');

class Touser extends Tomodel
{

   function Touser()
   {
      parent::Tomodel();
      $this->_ci->load->model('users');
   }

   function useradd()
   {
       $rules['username'] = 'required';
       $this->_ci->validation->set_rules($rules);
      
       if(!$this->_ci->validation->run())
       {
           return $this->_ci->validation->error_string;
       }
       else
       {
          $fields = array('user_name',$this->_ci->input->post('username'));
          $this->_ci->users->adduser($fields);
          return '';
       }
   }

}

It's not only useful for validation but also for uploads and other data related functions.




Theme © iAndrew 2016 - Forum software by © MyBB