[PHP] Generic Form Validation Class? (Full Version)

All Forums >> [Web Development] >> ASP and Database



Message


Ryokotsusai -> [PHP] Generic Form Validation Class? (3/29/2008 9:26:56)

Hello [:D]

I am working/playing with a generic form validation class, that should be able to validate any data sent to it. The only problem is I can only think of one *practical* way of determining how to validate each field, which requires the form fields have to have specific names, or keywords in their names (like loc_name, or area_num), or it will assume they are just generic text entries, and i would like to do away with that requirement.

So i guess my question is:

What is the best way to determine what kind of data *should* be in each form field without forcing the user to use specific keys in the field names when creating forms?

(hope that made sense)




ou812 -> RE: [PHP] Generic Form Validation Class? (3/29/2008 9:50:28)

I may not be understanding, but maybe you need to have the user also select, via a drop down, radio box etc., what type of field it is going to be, and then pass that along too. Every other type of data base or evaluation makes you say what type of data it is. I wouldn't try and perform the action on the field name though.

Not sure if if that even helps, or if I'm on the same track with you.




Ryokotsusai -> RE: [PHP] Generic Form Validation Class? (3/29/2008 10:26:44)

Sorry for not explaining it clearly (its still before noon XP),

This is part of a CMS project, and I would like it to be easy for whoever is operating a this to add custom forms, via a form to create a form, or enter the html directly, and that is where the problem would be. Say I am running a site with this (which i will be eventually) and I have a form I made in, say, FP to replace the standard registration. If I copy and paste this code into the site:

			<form action="http://www.codeburst.org/?pid=-1&method=full_register" method="post" id="full_register">
			
				<h2>Register a new Account</h2>
			
				<fieldset>
			
					<legend>Account Info</legend>
			
					<p>
			
						<span class="italic red">Required</span>
			
					</p>
					<p title="Allowed Characters: A-z 0-9 [] () | : . <> - _ ~ * + =">
			
						<label for="freg_uname">Desired Username:</label>
						<input type="text" name="uname" id="freg_uname" maxlength="48" value="" />
			
					</p>
					<p>
			
						<label for="freg_email">Email Address:</label>
						<input type="text" name="email" id="freg_email" maxlength="48" value="" />
			
					</p>
					<p>
			
						<label for="freg_pass1">Password:</label>
						<input type="password" name="pass1" id="freg_pass1" maxlength="48" value="" />
			
					</p>
					<p>
			
						<label for="freg_pass2">Verify Password:</label>
						<input type="password" name="pass2" id="freg_pass2" maxlength="48" value="" />
			
					</p>
			
				</fieldset>
				<fieldset>
			
					<legend>Personal Information</legend>
			
					<p>
			
						<span class="italic">Optional</span>
			
					</p>
					<p>
			
						<label for="freg_rname">Real Name:</label>
						<input type="text" name="rname" id="freg_rname" maxlength="48" value="" />
			
					</p>
					<p>
			
						<label for="freg_date">Birth Date:</label>
						<input type="text" name="bday" id="freg_date" maxlength="10" value="" />
			
					</p>
					<p>
			
						<label for="freg_map">Location:</label>
						<input type="text" name="location" id="freg_map" maxlength="48" value="" />
			
					</p>
					<p>
			
						<label for="freg_bio">About Me:</label>
						<textarea name="bio" cols="30" rows="5" id="freg_bio"></textarea>
			
					</p>
			
				</fieldset>

				<p>
					<input type="submit" value="Register" class="floatright" />
				</p>

			</form>


some of that will work as some of the keys are obvious like the key for email fields is 'email'..., but for half of those fields, the validation script will have no idea what they are and will default to *Generic Text Entry* and may flag some of the entered data as invalid...

if it helps here is the part of the validation script that decides how to validate the input, and again, this is the only *practical* way i could think of to do this:
(its not done yet, i still have to add for radio buttons, selects, etc...)
	function __construct($array) {

		foreach($array as $k => $v) {

			if(strpos($k,'email')) {

				email($v,$k);

			} elseif(strpos($k,'name')) {

				name($v,$k);

			} elseif(strpos($k,'date')) {

				date_($v,$k);

			} elseif(strpos($k,'num')) {

				num($k,$v);

			} else {

				gen_text($v,$k);

			}
		}

	}


also this seems like it could be slow for larger forms, so another way would help that too...




rdouglass -> RE: [PHP] Generic Form Validation Class? (3/30/2008 8:14:44)

If it helps, I've done generic form handlers and I identify the field type by using the first 3 letters of the form field to identify the field type. Something like:

txtFirstName
datBirthday
chkIsUSCitizen
radGender

Then I loop thru the form collection and check the first 3 charaters of the field name to decide how to deal with the field contents.

I have ASP examples if you like but nothing in PHP. Altho I'm pretty sure there should be no problem with the logic of this in PHP. Hope it helps.




Page: [1]

Valid CSS!




Forum Software © ASPPlayground.NET Advanced Edition 2.4.5 ANSI
0.09375