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

I am creating validation rules in model.

Goal is that all data manipulation functionality should be in model.

Normal validate without callback working perfect. But with callback function it is not working.

This functions are in my User_model.php

Code:
public function rules($action=""){

        $this->form_validation->set_rules('trigger_name',  "trriger", 'trim|required');
        $this->form_validation->set_rules('from_user',     "from",     'trim|required');
        $this->form_validation->set_rules('to_user',       "to",       'trim|required');
        $this->form_validation->set_rules('email',       "email",'callback_validate_user');
}

public function validate_user($str){
     $this->form_validation->set_message('email', 'Invalid user ');
     return false;
}

public function save(){
        $this->rules();

        if ($this->form_validation->run()){
           // save query
        }

}
Reply
#2

In CI 2.x I don't believe it was possible to use a callback method from the model. It always looked in the Controller.

However - the solution is to have a function in a helper that you can load prior to running the validation. Then, you treat it as a normal PHP function and not a callback, changing that line to:

Code:
$this->form_validation->set_rules('email',       "email",'validate_user');

Another solution, to keep the code in the same place, and break a few rules about organizing code in the process Smile is to simply include that function in the same file as the model. It's not ideal, obviously, but it does work (just don't tell anyone I recommended that. ha!)
Reply
#3

(01-09-2015, 04:59 AM)[email protected] Wrote: Goal is that all data manipulation functionality should be in model.

There's a difference between validation and manipulation ...
Reply
#4

(01-09-2015, 10:39 AM)Narf Wrote:
(01-09-2015, 04:59 AM)[email protected] Wrote: Goal is that all data manipulation functionality should be in model.

There's a difference between validation and manipulation ...

The best approach is to get all data validate from controller once it is all good then call the save method form model. In you case
I would do something like

class user extends CI_Controller(){

function __construct(){
parent::construct();
$this->load->model('user_model); //assume that you havent auto loaded then we can load here
$this->load->library('form_validation);
}
/* Assume here is the controller for showing user form
*
*/
function index(){
//put all validation code here
$this->form_validation->set_rules('trigger_name', "trriger", 'trim|required');
$this->form_validation->set_rules('from_user', "from", 'trim|required');
$this->form_validation->set_rules('to_user', "to", 'trim|required');
$this->form_validation->set_rules('email', "email",'callback_validate_user');
//fail validation
if($this->form_validation->run()===FALSE){
$this->load->view('yourformview', array()); // option array which you can pass something to view
}
else{ // all validation ok then
$post_data =$this->input->post(NULL,TRUE); //grab all data from form submission and sanitized with XSS, this is //optional
$this->user_model->save($post_data);
}

}//end index func
//here is your call back
// I just copy your code here but you may think of passing email to user_model to check if it is valid email
// then return FALSE or TRUE accordingly
// suggesting like $this->user_model->check_email($str) in which return boolean true or false
//
public function validate_user($str){
//your code just blindly return false
$this->form_validation->set_message('email', 'Invalid user ');
return false;
}//end callback


}//end class

I hope this helps
Reply
#5

@dbui

I use the same approach, a callback wrapper within the controller with a tiny difference (tried on CI3):

Code:
$this->form_validation->set_rules('email', "email",'callback_validate_user');

In my code it would be:

Code:
$this->form_validation->set_rules('email', "email",'callback__validate_user');

And:

Code:
public function validate_user($str) {

would be:

Code:
public function _validate_user($str) {

Ugly a little bit, but this prefixed with "_" callback rule can not be called by the router.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB