Page 1 of 1
Comparing variable names
Posted: Tue Apr 11, 2006 11:21 am
by jwalsh
I have a form that submits two types of data. One is the value, and one is a validation rule that maps to my validation class.
Ex.
Code: Select all
$_POST['FirstName'] = "Users Name";
$_POST['valFirstName'] = "required";
$_POST['Email'] = "foo@bar.com";
$_POST['valEmail'] = "email";
Whats the best way to loop through the $_POST array, and separate the validation elements from the value elements? Or should I be accomplishing this a different way entirely?
Posted: Tue Apr 11, 2006 11:48 am
by timvw
Basically, if i only post values... Your code wouldn't perform any validation on it? Doesn't seem very wanted...
I usually have an array with $expected_values = array('name', 'email', 'birthdate', ...)
Now, if i wanted to attach validation rules i'd probably make that a multi-dimensional array like:
Code: Select all
$expected_values = array();
$expected_values['name'] = array('requirerule', 'maxlength' => 5, 'minlenght' => 2);
...
And then i would iterate through that loop, see if the value exists in $_POST and apply the rules...
Posted: Tue Apr 11, 2006 11:56 am
by jwalsh
Sorry, I'm a bit confused. I think it's the way I described it. I do have an expected values validation rule. The form is generated based off component classes.
Ex.
Code: Select all
// function TextBox($name, $value = null, $size = 25, $maxLength = 25, $validationRule = "required", $cssStyle = null)
$FirstName = new TextBox("FirstName", $ThisUser->FirstName,15,40);
Generates..
Code: Select all
<input type="text"
name="FirstName"
size="15"
maxlength="40"
value="Entered Name"
class=""
/>
<input type="hidden"
name="valFirstName"
value="required"
\>
So there's really no way a validation rule couldn't be passed.
The idea is to build a reusable validation module for simple database access routines. Pre-existing ones I found seem to be very bloated.
Posted: Tue Apr 11, 2006 11:58 am
by timvw
jwalsh wrote:Sorry, I'm a bit confused.
Generates..
Code: Select all
<input type="text"
name="FirstName"
size="15"
maxlength="40"
value="Entered Name"
class=""
/>
<input type="hidden"
name="valFirstName"
value="required"
\>
So there's really no way a validation rule couldn't be passed.
How do you know everyone is going to post that html? What happens if someone copies the html without the hidden fields and posts from there?
Posted: Tue Apr 11, 2006 12:03 pm
by jwalsh
Ok, so you make a good point

. So setting a validation rule in the html is a bad idea. I'll create a validate() function in the class for each object.
Thanks,
Josh