• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
form validation where form vars are an array

In my form I have named my inputs as so:
<input type="text" name="guest[first_name]" />
<input type="text" name="guest[last_name]" />
<input type="text" name="guest[email]" />

With the validation library, they say to define fields like:
$rules['first_name'] = "required";
$rules['last_name'] = "required";
$rules['email'] = "required";

So the problem is that when it goes to validate, it's expecting to find $_POST['first_name'], etc.

So my question is, is there syntax to define a field where it is in a form array? (e.g. name="guest[first_name]").
I've tried:
$rules['guest[first_name]'] = "required";
$rules['guest']['first_name'] = "required";
...and neither worked.

Any help appreciated!

Sometime in the past I believe to remember that someone wrote an adaption of the Validation library to support arrays. A forum or wiki search might reveal more information.

I have use two methods to deal with this in the past:

1. Write a callback function which performs validation of the entire array and writes back the various results into the Validation instance. So in your case it would be a like callback_guest routine.

2. Flatten the form fields to something like: guest_first_name, and write rules against that. This option obviously requires that you do some post processing if you need to store this data in database, etc.


Check out


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.