Form_validation - checking checkboxes problem. Any help appreciated! (CI 1.7) |
[eluser]hugle[/eluser]
Hello everyone. I've been playing for a long time with form validation. But came with problems while checking checkboxes and can't handle them myself. So i wrote my own 'rule': In controller I have: Code: $this->form_validation->set_rules('terms', 'Terms', 'checkbox_terms'); And in application/libraries/MY_Form_validation.php: Code: <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); The problem is that this won't work at all. I tried to do a callback: $this->form_validation->set_rules('terms', 'Terms', 'callback_checkbox_terms'); and wrote a function in the same controller: Code: function checkbox_terms() { and it WORKS! What I'm doing wrong with Extendind Form_validation ? I find it more comfortable to use "extensions" Thank you everyone!
[eluser]hugle[/eluser]
Hello again It seems that with the version of CI from SVN: $this->form_validation->set_rules('terms', 'Terms', 'required'); this one works, I get an error (if unchecked the terms): 'The Terms field is required.' Now I need to figure out, how do I change this message to custom one. Thanks for CI cheers, CI loving user
[eluser]crumpet[/eluser]
You need to use this command $this->form_validation->set_message('rule', 'Error Message'); unfortunately it overrides all of the instances of that rule so if you used required elsewhere it will display the text you change it to.. try using isset for checkboxes (that is what the basis for the required rule is anyways) and then change the error message for isset to whatever you want.
[eluser]hugle[/eluser]
[quote author="crumpet" date="1227259080"]You need to use this command $this->form_validation->set_message('rule', 'Error Message'); unfortunately it overrides all of the instances of that rule so if you used required elsewhere it will display the text you change it to.. [/quote] Agree. That is why I don't change it in this way. I'm gonna play with it soon. Quote: try using isset for checkboxes (that is what the basis for the required rule is anyways) and then change the error message for isset to whatever you want.Just tried your suggestion: changed from: Code: $this->form_validation->set_rules('lookingfor', 'Looking for', 'required'); Code: $this->form_validation->set_rules('lookingfor', 'Looking for', 'isset'); Strange ... Any thought on this one ? Thanks for your time crumpet
[eluser]hugle[/eluser]
Hello. as for a moment I came with one sollution on how to change an error message for one specific rule. for example I have a rule : Code: $this->form_validation->set_rules('terms', 'terms', 'required'); So now I replace it: Code: $data['errors'] = str_replace('The terms field is required.', 'You must accept terms and conditions', validation_errors()); and later in the view file, I echo data['errors'] whitch contains all the validation errors and the "custom" terms error If anyone has better sollution - please share
[eluser]daweb[/eluser]
I have the same problem. Why not, in the view file: Code: if( form_error('yourfile') ) echo 'Your message';
[eluser]sholsinger[/eluser]
[quote author="hugle" date="1227254639"] So i wrote my own 'rule': In controller I have: Code: $this->form_validation->set_rules('terms', 'Terms', 'checkbox_terms'); And in application/libraries/MY_Form_validation.php: Code: <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); I believe the documentation states that any custom validation function must require a single argument. For instance, your function would become: Code: <?php Although, after reviewing the Form_validation class it looks like the field's rule must be: 'callback_checkbox_terms' in conjunction with the function above. Note the 'callback_' portion. That is required for callbacks to work. (Which is also stated in the User Guide) Here's how callbacks work with CI 1.7.0: First, Form_validation's _execute() method checks to see if the callback function exists as a method of CodeIgniter's core. Second, it checks to see if the method exists within '$this'. Third, if all else fails, check to see if the function is globally scoped. Finally, if _execute() cannot find the callback function, it just ignores it. I highly suggest you check out the User Guide if you get a chance. |
Welcome Guest, Not a member yet? Register Sign In |