CodeIgniter Forums
Putting validation rules in a model? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Putting validation rules in a model? (/showthread.php?tid=27346)



Putting validation rules in a model? - El Forum - 02-08-2010

[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.


Putting validation rules in a model? - El Forum - 02-08-2010

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



Putting validation rules in a model? - El Forum - 02-08-2010

[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?


Putting validation rules in a model? - El Forum - 02-09-2010

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