Welcome Guest, Not a member yet? Register   Sign In
form validation where form vars are an array
#1

[eluser]Unknown[/eluser]
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!
#2

[eluser]Mirage[/eluser]
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.

HTH,
-m
#3

[eluser]Yash[/eluser]
Check out

http://ellislab.com/forums/viewthread/51260




Theme © iAndrew 2016 - Forum software by © MyBB