![]() |
Form Validation - Array Count - 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: Form Validation - Array Count (/showthread.php?tid=58216) |
Form Validation - Array Count - El Forum - 05-24-2013 [eluser]Unknown[/eluser] Hi, I'm fairly new to CodeIgniter and I'm having an issue with the form validation library. What I'm trying to accomplish is, There are 10 types of Electronic Gadgets and a user is allowed to select minimum of 1 and a maximum of 4. These are checkboxes. In native PHP I could simply do this by Code: $selected_items_count = $_POST['gadget']; But then again, I'm using CodeIgniter ![]() Thanks a lot in advance. Form Validation - Array Count - El Forum - 05-24-2013 [eluser]TheFuzzy0ne[/eluser] Welcome to the CodeIgniter forums! Interesting problem. What I would suggest you do, is instead of trying to validate each checkbox, you add a hidden form field and add your validation rule to that. For example, let's add a hidden form field, and call it "_gadget". So the validation rule would be something like: Code: // Note the TWO underscores. This is so we can make our validation method innaccessible Now we just need a method to do the magic. I assume you're submitting your checkboxes as a single array: Code: function _validate_gadgets() Just remember, in your view you need to show any errors for the "_gadget" field, not the "gadget" field. Code: <?php echo form_error('_gadget'); ?> I don't expect that to work out of the box, but hopefully you can see what we're doing. We're validating the checkboxes, but using the hidden form field as the trigger. It seems a bit awkward at first, but once you get your head around what's going on, it's surprisingly simple. Please let me know if you need more details. I'm up to my ears in work at the moment, so I didn't want to do any unnecessary typing. Form Validation - Array Count - El Forum - 05-24-2013 [eluser]Unknown[/eluser] Thanks a lot !! That's a great solution ![]() ![]() |