CodeIgniter Forums
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'];
if ( count( $selected_items_count) >= 1 && count( $selected_items_count) <= 4 ) {
....
}

But then again, I'm using CodeIgniter Smile I want to use the form validation library to accomplish this. I tried creating a callback function but each element in the $_POST["gadget"] is getting passed to it separately and not as the array itself. Can someone kindly help me out !

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
// to the outside world by prefixing it with an underscore.
$this->form_validation->set_rules('_gadget', 'gadget', 'callback__validate_gadgets');

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()
{
    // Count how many gadgets are in the array.
    // Checkboxes won't submit any value if they aren't checked. Handy!
    $num_gadgets = count($_POST['gadget']);

    if ($num_gadgets == 0 OR $num_gadgets > 4)
    {
        $this->form_validation->set_message('_validate_gadgets', 'You must select between 1 and 4 gadgets.');
        return FALSE;
    }
    
    // Maybe add some more validation here, just to check that the submitted values
    // are valid to prevent database errors.

    return TRUE;
}

Just remember, in your view you need to show any errors for the "_gadget" field, not the "gadget" field.

Code:
&lt;?php echo form_error('_gadget'); ?&gt;

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 Smile Thanks again Smile