Welcome Guest, Not a member yet? Register   Sign In
form_validation library with input arrays
#1

[eluser]coffeeandcode[/eluser]
Hi all, I'm having difficulties getting ci's form_validation library to work with an input array I have. My variable is an array of text inputs containing product quantities, indexed by product id:

Code:
Array
(
    [quantities] => Array
        (
            [155530] => 4
            [252605] => 2
            [295539] => 6
            [176950] => 3
            [155531] => 9
            [155529] => 0
            [155528] => 9
            [155526] => 1
            [258971] => 9
            [258967] => 3
            [258972] => 1
            [161637] => 7
            [161631] => 3
        )

)

The problem is, ci requires that the EXACT variable name is used everywhere, and in my case that is dynamic. I can get it to validate when I use "quantities[]" as the variable name, but then I lose the product id. Am I missing something? I would think this would be a simple thing to do...
#2

[eluser]Jondolar[/eluser]
Can you create a callback function when validating the quantities variable and then validate the individual array items yourself? I think I already had this issue and may have already solved this. I'll check when I get home.
#3

[eluser]coffeeandcode[/eluser]
[quote author="Jondolar" date="1282620184"]Can you create a callback function when validating the quantities variable and then validate the individual array items yourself? I think I already had this issue and may have already solved this. I'll check when I get home.[/quote]

I can, but I can only get it to match the variable if I put the product ids into the form validation config:

Code:
$config["orders/bulk"] = array(
    array(
        "field" => "quantity[155530],
        "label" => "quantity for product #155530",
        "rules" => ""
    ),
    array(
        "field" => "quantity[252605],
        "label" => "quantity for product #252605",
        "rules" => ""
    ),
    ...
)

And then it treats them each as separate variables and validates them separately. This doesn't work because I want it to validate when the user has entered at least one product, of any id. ie, it should validate if one of the product quantities is greater than 0. I need it to be treated as a single variable.

I also tried putting the ids and quantities into 2 different branches of a 2 dimensional array:

Code:
<input class="quantity" type="text" size="1" name="quantities[quantity][]" value="<?php echo set_value("quantities[quantity][]") ?>"  />
<input type="hidden" name="quantities[id][]" value="<?php echo $item["id"] ?>"  />

and in this case it triggers the form validation properly, but it is still validating each array element separately. And it is a lot messier than what I was doing without the form validation library.

Am I missing something? Is this not the way html input arrays are supposed to be used? Is there a cleaner way of doing what I'm trying to accomplish?




Theme © iAndrew 2016 - Forum software by © MyBB