Welcome Guest, Not a member yet? Register   Sign In
Model or Controller - Where to Validate
#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.


Messages In This Thread
Model or Controller - Where to Validate - by El Forum - 04-13-2008, 04:21 PM
Model or Controller - Where to Validate - by El Forum - 04-13-2008, 04:28 PM
Model or Controller - Where to Validate - by El Forum - 04-13-2008, 04:32 PM
Model or Controller - Where to Validate - by El Forum - 04-13-2008, 04:50 PM
Model or Controller - Where to Validate - by El Forum - 04-13-2008, 06:04 PM
Model or Controller - Where to Validate - by El Forum - 04-13-2008, 06:42 PM
Model or Controller - Where to Validate - by El Forum - 04-13-2008, 11:54 PM



Theme © iAndrew 2016 - Forum software by © MyBB