Do validation callbacks work inside models ? |
[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?
[eluser]Popcorn[/eluser]
Can you provide some code samples and I'll see if I can help you.
[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 {
[eluser]Monkeytail[/eluser]
The code as requested =) CONTROLLER: Code: class Songs extends Controller { MODEL: Code: class Song extends DataMapper { 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.
[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
[eluser]pistolPete[/eluser]
You could have a look at MY_Validation_-_Callbacks_into_Models.
[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
[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);
[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){ However my function is still not being picked up by the form_validation. Any suggestion on what am I missing here? thanks
[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) 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. |
Welcome Guest, Not a member yet? Register Sign In |