Callback doesn't work, what's wrong? |
[eluser]CroNiX[/eluser]
Repost what you currently have now. -relevant view portion showing your select and outputting the form error -relevant controller where you are setting validation rules and also the callback function
[eluser]ReyPM[/eluser]
Ok, here: relevant part of the view, where error should be displayed and where select element is render Code: <p><label>Código UNESCO:</label> relevant part of the controller where logic happens: Code: public function create() { the callback function Code: public function check_unesco($value) {
[eluser]CroNiX[/eluser]
Glad it's working. A few tips. In your callback, you can use %s in the error message string and it will be replaced with the field name that you enter as the 2nd parameter when using Code: $this->form_validation->set_rules($actual_field_NAME, $text_to_display_as_field_name, $rules); So if Code: $text_to_display_as_field_name = 'Código UNESCO'; Code: $this->form_validation->set_message('check_unesco', 'Debe seleccionar un %s'); Quote:Debe seleccionar un Código UNESCOWhich allows you to use the same rule on many different fields, but have the error message custom to that individual field that it is displayed next to so it can be reused. Another thing. If this is a rule that you might want to use over and over again, perhaps in another controller or even in another CI project, then it would be best to extend the form validation library with your own rules, which essentially work the same as callbacks except they are native rules just like the built in validation rules. Here's how to extend a Native CI Library. It's pretty simple though. All you'd do is create a file in /application/libraries/MY_Form_validation.php Code: class MY_Form_validation extends CI_Form_validation { Then the only other difference would be in your controller, where you set the validation rules. You just don't need to use "callback_" prefixing the rule name now since it is a native validation rule: Code: $this->form_validation->set_rules('id_unesco', 'Código UNESCO', 'trim|check_unesco|xss_clean'); Everything else would be the same. You don't need to do anything special to load these validation rules. You still just Code: $this->library->load('form_validation'); Now, if you need to use this rule in another controller you don't need to recreate the callback rule there. You just use this one. And if you have another CI project that you want to use the rule(s) on, you can just copy that /application/libraries/MY_Form_validation.php file to that project and all of your custom rules are now available. Pretty handy.
[eluser]ReyPM[/eluser]
Excellent post CroNix and very helpful I'll take into account in this project and the new one starting right now! |
Welcome Guest, Not a member yet? Register Sign In |