0

I have group of text boxes like below

<input type="text" size="10" value="<?php echo set_value('sizes[]'); ?>" name="sizes[]"/>
<input type="text" size="10" value="<?php echo set_value('sizes[]'); ?>" name="sizes[]"/>
<input type="text" size="10" value="<?php echo set_value('sizes[]'); ?>" name="sizes[]"/>
<input type="text" size="10" value="<?php echo set_value('sizes[]'); ?>" name="sizes[]"/>
<input type="text" size="10" value="<?php echo set_value('sizes[]'); ?>" name="sizes[]"/>

then if I set validation rules like below

array(
'field'   => 'sizes[]',
'label'   => 'SIZES',
'rules'   => 'required|xss_clean'
),

then codeigniter check every input box for value. If every text box is filled up then codeigniter returns true. But I want to get if anyone of them filled up then return true

How can I do that??

Thanks

4
  • why do you use name="sizes[]" for html element's name?. Commented Dec 26, 2012 at 14:18
  • You can handle above one simply with a "||" (or) condition. but I can't understand your naming convention for html elements. Commented Dec 26, 2012 at 14:20
  • It is not quite clear from your code what do you want to achieve... Do you want to store the names of fields in the array sizes[]? Commented Dec 26, 2012 at 14:34
  • There are five text boxes. I would like to save their values in database.At time of form validation codeigniter check every input box for value. If every text box is filled up then codeigniter returns true. But I want to get if anyone of them filled up then return true Commented Dec 26, 2012 at 17:18

1 Answer 1

1

You can create a custom validation function in your controller and implement your own check on the input. First, you add a new function to the controller:

function _is_valid($str, $field_name)
{
    //First we see if the field name we are checking has multiple values
    if (!is_array($this->input->post($field_name))) 
        return false;

    //Let's get the posted values and see if any of them has a value:
    foreach ($this->input->post($field_name) as $value)
        if (strlen($value)) return true;

    //If we get here that means there was no value posted:
    return false;   
}

Next, change your rules for the field:

'rules'   => 'required|xss_clean' 

To:

'rules'   => 'callback__is_valid[sizes]|xss_clean'

You can read more about custom validation functions here:

[http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html#callbacks][1]

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.