CodeIgniter Forums

Full Version: Validating Array Size in Call Back Function
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2

El Forum

[eluser]markusr[/eluser]
Hopefully this is a simple question, but I am trying to create a custom validation function using the callback feature in CI. I have multiple checkboxes representing category choices, and I would like to check to make sure that no more than 4 categories were selected. I tried check the size of the array in my callback function, but it seems as if callback does not pass and array it only passes strings. Is there a work around for this? Below is some example code to clarify.

Code:
function count_category($array){
if(count($array)> 4){
return false;
$this->form_validation->set_message('count_category', '4 Category Max. Please try again.');
}
else{
return true;
}
}
If I print the count($array) it prints the number 1, 4 times.

Thanks in Advanced

El Forum

[eluser]TheFuzzy0ne[/eluser]
The form validation library never passes an array to the validation function, but a string. Technically, you will only need to attach a custom validation callback to just the name of the array. For example:

Code:
# Let's say you have some HTML like this
<input type="text" name="myinput[]" value="" />
<input type="text" name="myinput[]" value="" />
<input type="text" name="myinput[]" value="" />

# Then you'd set your validation rules like this:
$this->form_validation->set_rules('myinput', '', 'callback_count_category');

# And your callback might look like this:
function count_category($array){
        $input_array = $this->input->post('myinput');
        $count = 0;
        
        # Even if the form inputs were not filled in, they still exist in the array as "",
        # so let's loop through the array.
        
        foreach ($input_array as $entry)
        {
            # Does it contain a value other than ""?
            if ($entry != "")
            {
                $count++; # Increment the count.
            }
        }
        
        if($count > 4)
        {
            $this->form_validation->set_message('count_category', '4 Category Max. Please try again.');
            return false; # You need to return false AFTER you set the message. :)
        }
        return true;
    }

I haven't tested it, but I can't see why it shouldn't work. I'm not sure how the form validation library handles arrays with preset values such as "required", however, which is why I always create custom callback functions for my form arrays.

El Forum

[eluser]markusr[/eluser]
It works!!! Thanks!!!. Now I'm getting an Unable to access an error message corresponding to your field name, instead of my error message. It's been happening with all my callback functions. I am using the new form_validation library. Any clue?

El Forum

[eluser]TheFuzzy0ne[/eluser]
Are you still returning FALSE before you've set the message?

El Forum

[eluser]markusr[/eluser]
No. Message then False. Still getting the error.

El Forum

[eluser]TheFuzzy0ne[/eluser]
Does your controller extend the Controller class? You are defining your callback in your controller, right?

El Forum

[eluser]markusr[/eluser]
Yes:
Code:
class Nominate extends Controller {

    function Nominate()
    {
        parent::Controller();    
        $this->load->helper(array('form', 'url', 'text', 'date'));
        $this->load->library(array('form_validation', 'email', 'session'));
        
    }

and I am defining my callback in this controller

Code:
function count_category($array)
    {
      $input_array = $this->input->post('category');
          $count = 0;
        foreach ($input_array as $entry)
        {
            if ($entry != "")
            {
                $count++;
            }
        }
        
        if($count > 4)
        {
            $this->form_validation->set_message('count_category', '4 Category Max. Please try again.');
            return false;
        }
        return true;
    }

and my rule looks like this

Code:
$this->form_validation->set_rules('category', 'Category', 'required|callback_count_category');

El Forum

[eluser]TheFuzzy0ne[/eluser]
I'd suggest removing the "required" rule, and implementing it within your custom callback. Basically, you just need to check that $count != 0. If it is zero, set the message and return FALSE.

El Forum

[eluser]markusr[/eluser]
Still not working.

El Forum

[eluser]markusr[/eluser]
OK I got it. The only way I could fix it was to remove form_validation from parent the controller and auto loaded it globally. CRAZY!! Thanks to this thread. Seems like a bug in the form_validation class.
Pages: 1 2