Welcome Guest, Not a member yet? Register   Sign In
Loading form validation
#1

[eluser]frist44[/eluser]
I'm trying to slim down the amount of code in my controllers and I've been thinking about form validation. Is it standard practice to load validation rule sets via the model in a fashion like this:

Code:
class Users_member_model extends Model
{
    private $_CI;
    
    function __construct()
    {
        parent::__construct();
        
        $this->_CI = &get;_instance();
        $this->_CI->load->library('form_validation');    
        
        $this->_init_validation_rules();
    }
    
       function _init_validation_rules() {
        $this->_CI->form_validation->set_rules('fname', 'first name', 'trim|required');
          $this->_CI->form_validation->set_rules('lname', 'last name', 'trim|required');
          $this->_CI->form_validation->set_rules('address1', 'address', 'trim|required');
        $this->_CI->form_validation->set_rules('city', 'city', 'trim|required');
        $this->_CI->form_validation->set_rules('state', 'state', 'trim|required');
        $this->_CI->form_validation->set_rules('zip', 'zip', 'trim|required|numeric');
        $this->_CI->form_validation->set_rules('email', 'email', 'trim|required|valid_email');
        $this->_CI->form_validation->set_rules('emailconf', 'email confirmation', 'trim|required|valid_email|matches[email1]');
        $this->_CI->form_validation->set_rules('username', 'username', 'trim|required|min_length[6]');
        $this->_CI->form_validation->set_rules('password', 'password', 'trim|required|min_length[6]');
        $this->_CI->form_validation->set_rules('passwordconf', 'password confirmation', 'trim|required|min_length[6]|matches[password1]');
    }

}

Or is it better to set all the rules in the controller. It just seems to clutter the controller with this many rules.

Thanks!
#2

[eluser]flaky[/eluser]
the model isn't intended to do validation on the data, since model only is for data access, while in the controller you can do the data validation and app logic.
#3

[eluser]rogierb[/eluser]
@flaky, @frist44,
You can do validation in your model. It all depends on how you read or interpret the MVC principle.

There are those that want 'big' models and 'small' controller and vice versa.

Personaly, I want validation to be done by the controller since validation can differ from method to method.




Theme © iAndrew 2016 - Forum software by © MyBB