Welcome Guest, Not a member yet? Register   Sign In
Do validation callbacks work inside models ?
#1

[eluser]Monkeytail[/eluser]
I'am trying to slim down my controllers, therefore I'am moving the validation flow into my models.
My self-made validation functions (callbacks) don't seem to work.. from inside a controller they do.
Is this normal behaviour?
#2

[eluser]Popcorn[/eluser]
Can you provide some code samples and I'll see if I can help you.
#3

[eluser]bitist[/eluser]
Callback functions are accessible only from that class where it supposed to be used.

But you can make a little hack to access the model's class function:

Code:
class X {
   function Y() {
       $this->load->library('validation');
       $rules['captcha'] = 'required|callback_captcha_check'; //callback request
   }

   function captcha_check() {
       $this->your_model->captcha_check();
   }
}
#4

[eluser]Monkeytail[/eluser]
The code as requested =)

CONTROLLER:
Code:
class Songs extends Controller {
  
    function __construct() {
      parent::Controller();
      $this->load->helper(array('html', 'form', 'url'));
      $this->load->library('form_validation');
    }
    
    function add_song() {
      $s = new Song();
      $s->artist = $this->input->post('artist');
      $s->ranking = $this->input->post('ranking');
      if($s->validate_song_entry() ) {
        $s->save();
        redirect('songs/add_song');
      } else {
        $data['title'] = 'Add new song';
        $this->load->view('add_song_v', $data);        
      }
    }
    
}

MODEL:
Code:
class Song extends DataMapper {
  
    function __construct() {
      parent::DataMapper();
      $this->load->library('form_validation');
    }
    
    function validate_song_entry() {
      $this->form_validation->set_rules('artist', 'Artist', 'required');
      $this->form_validation->set_rules('ranking', 'Ranking', 'required|callback_digit_check');
      return($this->form_validation->run() === false) ? false : true;
    }
    
    function digit_check($digit) {
      $this->Songs->digit_check($digit);
      if($digit > 10) {
        $this->form_validation->set_message('digit_check', /* message */);
        return false;        
      } else {
        return true;
      }
    }

}

Some 'native' DataMapper validation functionality - that promotes validation flow in models (I like that) - act's a bit different. I like to stick as much to the CI way of input validation.
#5

[eluser]creaflexi[/eluser]
Hi, I am experiencing the same problem. When doing the validation from withing the Model I can use all the built in validation functions such as 'required|min_length[]' etc. However when use my own function with the "callback_" prefix, it does not get executed. I understand that I could call this function when doing the validation from within the Controller.
I don't like the idea to have all the validation done within the Controller class, to my understanding Controller is there just to pass the queries to Models and then return whatever data from Model to View - (correct me if I am wrong please).
That way the Controller is clean of any processing, that including the data validation.

Would there be any way around this? i.e. using the own defined validation functions when performing the validation from within the Model.

Thank you
#6

[eluser]pistolPete[/eluser]
You could have a look at MY_Validation_-_Callbacks_into_Models.
#7

[eluser]creaflexi[/eluser]
Thanks for your reply. I am not 100% sure what steps I should take now. I am not very experienced with CI so far.
I have extended the Validation class and saved it as MY_Validation.php (do I still load the Validation.php class)
From the WIKI page you gave me:
Quote:This is a more advanced version of the previous callbacks into models extension. You must pass the calling object to the Validation class. ie: $this->validation->run($this);

Any suggestions for what is "calling object"??

Thanks
#8

[eluser]pistolPete[/eluser]
Are you using the newer Form_validation library? If so this extension has to be updated...

Quote:Any suggestions for what is “calling object”??
This "calling object" is the CodeIgniter instance.
In your controller you invoke the validation using:
Code:
$this->validation->run($this);
#9

[eluser]creaflexi[/eluser]
Yes I am using Form_validation. I have extended it to MY_Form_validation as suggested in
http://codeigniter.com/wiki/MY_Form_vali...to_Models/

I am calling the function from within the method as follows:
Code:
$this->form_validation->set_rules('username', 'Username', 'required|callback_users_model->is_unique[username]');

running the validation:
Code:
$this->form_validation->run($this)

and finally my own defined function:

Code:
function is_unique($username){
echo "debug from within the defined function";
//check the db
}

However my function is still not being picked up by the form_validation. Any suggestion on what am I missing here?
thanks
#10

[eluser]TheFuzzy0ne[/eluser]
You don't actually need the [username] part, as the username is automatically passed as the first parameter to your callback.

What I like to do, is create an alias in the controller that passes the data to the appropriate function.
Code:
function _unique_username($str)
{
    return $this->users_model->is_unique($str);
}

and your validation rules:
Code:
$this->form_validation->set_rules('username', 'Username', 'required|callback__unique_username]');

If your callback isn't working, then you might not have the model loaded.




Theme © iAndrew 2016 - Forum software by © MyBB