Welcome Guest, Not a member yet? Register   Sign In
Form validation of dynamic sized multi dimension POST array
#1

[eluser]deed02392[/eluser]
Hi guys, I've got a form that looks like this:

Code:
<input type="text" name="row[0][itemno]">
<input type="text" name="row[0][upc]">
<textarea name="row[0][description]"></textarea>
<textarea name="row[0][spec]"></textarea>

<input type="text" name="row[1][itemno]">
<input type="text" name="row[1][upc]">
<textarea name="row[1][description]"></textarea>
<textarea name="row[1][spec]"></textarea>

Users can increase the number of fields with an AJAX call which returns the HTML of additional form elements. The rule is that it 'grows' in size with the 'row' array's first dimension increasing contiguously.

I'm wondering how I can validate given this, as I don't see in the validation documentation it supports anything like a wildcard for arrays like this? But the main reason I'm asking is I wonder if there's a better alternative to this way of having multi dimension array anyway. Thanks! PS Sorry about the English.
#2

[eluser]CroNiX[/eluser]
Code:
<input type="text" name="itemno[]">
<input type="text" name="upc[]">
<textarea name="description[]"></textarea>
<textarea name="spec[]"></textarea>

<input type="text" name="itemno[]">
<input type="text" name="upc[]">
<textarea name="description[]"></textarea>
<textarea name="spec[]"></textarea>

$itemno = $this->input->post('itemno', TRUE);
print_r($itemno);

Array (
  0 => 'value from first block',
  1 => 'value from second block'
)
Edited to remove multidimensional input names as they aren't really needed.
#3

[eluser]deed02392[/eluser]
I wasn't sure it would be sufficient to do it this way as I was concerned that gaps in the form might mean that the elements of the multiple POST arrays would not align.

However I did some testing with a basic script and it seems, in Chrome, IE9 and FF 13 at least that even if an element is empty, it still gets submitted and seen in PHP as an empty string (text input elements).

So this seems like a safe enough method for now, although I haven't checked the RFC to see if it is completely reliable to expect this from the majority of browsers.




Theme © iAndrew 2016 - Forum software by © MyBB