Form Validation Extensions: conditional validation method - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22) +--- Thread: Form Validation Extensions: conditional validation method (/showthread.php?tid=22443) |
Form Validation Extensions: conditional validation method - El Forum - 09-09-2009 [eluser]Unknown[/eluser] Hi all, Because Codeigniter's form validation does not have a conditional rule (as of yet?), I decided to write one myself. This validation rule takes a comma-separated list of values without spaces. This piece of code can go directly in your system/libraries/Form_validation.php file: Code: /** This works great for validating field groups like phone numbers, when you have, as I do, three different fields for the user to enter a phone number. You can also assign group labels! In your system/application/config/form_validation.php file, the code for validating a phone field would be something like this: Code: ... Now, if one of those fields is empty, the validation will fail. It may fail multiple times, but it will only print one error message. The fields will also validate normally. Please also note that the label:<label> argument is optional, but must come last. Disclaimer: Every code has bugs! Use this method at your own risk. Enjoy! Form Validation Extensions: conditional validation method - El Forum - 10-19-2009 [eluser]leftyboy[/eluser] I like this idea but I can't make it work I've added the code to the system/libraries/Form_validation.php and then I'm calling this in my controller: $this->form_validation->set_rules('telephone', 'Telephone', 'needs[telephone,fax,label:phone]'); $this->form_validation->set_rules('fax', 'Fax', 'needs[telephone,fax,label:phone]'); When I submit a blank form I don't see errors for these 2 fields. Am I being dim? Form Validation Extensions: conditional validation method - El Forum - 02-17-2010 [eluser]Unknown[/eluser] It works for me as expected. However I did implement it slightly differently; wrapping it in a class: Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); and saving it to /system/application/libraries/MY_Form_validation.php as is recommended by the documentation. My (working) implementation: Code: array( If 'commercial_project' is set, it requires that project_name and project_company_name are also set. Form Validation Extensions: conditional validation method - El Forum - 10-18-2010 [eluser]codeninja[/eluser] How would we handle the radio buttons validation with this? |