Comparing variable names

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
jwalsh
Forum Contributor
Posts: 202
Joined: Sat Jan 03, 2004 4:55 pm
Location: Cleveland, OH

Comparing variable names

Post 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?
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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...
User avatar
jwalsh
Forum Contributor
Posts: 202
Joined: Sat Jan 03, 2004 4:55 pm
Location: Cleveland, OH

Post 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.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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?
User avatar
jwalsh
Forum Contributor
Posts: 202
Joined: Sat Jan 03, 2004 4:55 pm
Location: Cleveland, OH

Post 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
Post Reply