Validation - Custom callback not behaving as expected? - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Validation - Custom callback not behaving as expected? (/showthread.php?tid=5817) Pages:
1
2
|
Validation - Custom callback not behaving as expected? - El Forum - 02-04-2008 [eluser]NBrepresent[/eluser] Hi, I might just be doing this wrong... but I have a situation where one of two fields must be set, so neither are required, but it is required that at least one is set. Here's the custom callback I'm using: Code: //ratings check Here's how I set the rules: Code: $rules['us_rating'] = "numeric|callback_ratings_check"; Submitting the form with neither of them set is NOT returning false and triggering the error (as it should). Can anyone offer any advice on this? Validation - Custom callback not behaving as expected? - El Forum - 02-04-2008 [eluser]wiredesignz[/eluser] I usually set the error message before the test. Code: function ratings_check() { Of course if the numeric check fails first the callback won't be run EDIT: Validation is not carried out on an empty input unless the "required" switch is used. You may want to add the callback test to the submit button or some other field instead. Validation - Custom callback not behaving as expected? - El Forum - 02-04-2008 [eluser]tonanbarbarian[/eluser] i would try debugging this in stages first i would change the function to Code: function ratings_check() { Then once you know if it is being called you can further test the code Validation - Custom callback not behaving as expected? - El Forum - 02-24-2008 [eluser]Unknown[/eluser] I just experienced the same problem. One my fields is not required but I still want to validate it through a custom callback function. Quote:Validation is not carried out on an empty input unless the “required” switch is used. Is this a bug that should be reported? Or is this supposed to be normal behavior? I'd like to have someone's input on this. Validation - Custom callback not behaving as expected? - El Forum - 04-01-2008 [eluser]ardinotow[/eluser] [quote author="ianolito" date="1203898898"]I just experienced the same problem. One my fields is not required but I still want to validate it through a custom callback function. Quote:Validation is not carried out on an empty input unless the “required” switch is used. Is this a bug that should be reported? Or is this supposed to be normal behavior? I'd like to have someone's input on this.[/quote] I also have same problem. Callback function will not work on empty field so here is my trick: 1. Set the default value of your field that need to validate to '-' (without qoute) 2. Add this javascript to make sure that '-' will still the default value eventhough a user delete '-': onFocus="if (this.value=='-'){this.value='';};return false;" onBlur="if (this.value==''){this.value='-';return false;}" --> copy-paste the javascript to text or textarea identifier. Example: <textarea name="address" cols="40" rows="4" id="address" onFocus="if (this.value=='-'){this.value='';};return false;" onBlur="if (this.value==''){this.value='-';return false;}">-</textarea> Now, your callback function should be work! Validation - Custom callback not behaving as expected? - El Forum - 04-01-2008 [eluser]Silvrbak[/eluser] I was just about to post about this. The javascript is cool but I wouldn't want to have to use that in all my views. Any other ideas? Validation - Custom callback not behaving as expected? - El Forum - 04-01-2008 [eluser]ardinotow[/eluser] I have new solution. Instead of declare default value with javascript like I mentioned before, hacking the Validation library is seems to be work. Open Validation.php where located in system/libraries/Validation.php. In function run() find this line: Code: if ( ! isset($_POST[$field]) OR $_POST[$field] == '') Delete 'continue;' or just give double slash on it, here is that line should like: Code: if ( ! isset($_POST[$field]) OR $_POST[$field] == '') If you think this solution has potential problem or side effect please let me know.... Validation - Custom callback not behaving as expected? - El Forum - 03-21-2010 [eluser]jppi_Stu[/eluser] Came across this thread almost two years later, while learning about form validation with CI and needing a similar solution (at least one of two fields must be filled, but neither by itself is required). Using code similar to the OP's code, but with the current form_validation library, seems to work. It was necessary, however, to call it only on one of the two fields, otherwise the message would appear once for each field (which makes sense, of course). Validation - Custom callback not behaving as expected? - El Forum - 03-22-2010 [eluser]InsiteFX[/eluser] You should not be hacking the CodeIgniter Core! Instead extend the library and customize it. Enjoy InsiteFX Validation - Custom callback not behaving as expected? - El Forum - 03-22-2010 [eluser]jppi_Stu[/eluser] [quote author="InsiteFX" date="1269316718"]You should not be hacking the CodeIgniter Core![/quote] I assume this is in reply to ardinotow since neither the OP nor I am hacking the core... (?) |