Welcome Guest, Not a member yet? Register   Sign In
Putting validation rules in a model?
#1

[eluser]gh0st[/eluser]
I'm wanting to put my validation rules in a model, rather than in my controller; that way we keep to the rule of "skinny controllers, fat models" -- I've also seen in other frameworks where they put the rules for validation in the model.

However, I am unable to get it to work.

Code:
class News_m extends Model
{
    public function News_m()
    {
        parent::__construct();

        /*
        $this->load->library('form_validation');
        $this->form_validation->set_rules('username', 'Username', 'required');
        $this->form_validation->set_rules('password', 'Password', 'required');
        $this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
        $this->form_validation->set_rules('email', 'Email', 'required');
        */
        
        $this->load->library('form_validation');
        $this->form_validation->set_rules('password', 'Password', 'required');
        
    }


}


The above gives me an error:

Quote:Call to a member function set_rules() on a non-object

I've tried putting $this->CI->form_validation and even parent::form_validation->set_rules

Neither of these two work.

Is there another way to fix this issue?

Thanks.
#2

[eluser]flaky[/eluser]
Code:
$ci =& get_instance();
$ci->load->library('form_validation');
$ci->form_validation->set_rules('password', 'Password', 'required');
#3

[eluser]richzilla[/eluser]
Sorry to jump on someone else's post, but i to have been wondering how to do this for a while. im assuming theres no way to get the form data directly to the model? it would be case of a controller function that calls the necessary function in the model?
#4

[eluser]gh0st[/eluser]
Thanks for your help flaky!




Theme © iAndrew 2016 - Forum software by © MyBB