navigation
a webmaster learning community
     Home    Register     Search      Help      Login    
Sponsors

Shopping Cart Software
Ecommerce software integrated into Frontpage, Dreamweaver and Golive templates. No monthly fees and available in ASP and PHP versions.

Website Templates
We also have a wide selection of Dreamweaver, Expression Web and Frontpage templates as well as webmaster tools and CSS layouts.

Frontpage website templates
Creative Website Templates for FrontPage, Dreamweaver, Flash, SwishMax

Search Forums
 

Advanced search
Recent Posts

 Todays Posts
 Most Active posts
 Posts since last visit
 My Recent Posts
 Mark posts read

Microsoft MVP

 

[PHP] Generic Form Validation Class?

 
View related threads: (in this forum | in all forums)

Logged in as: Guest
Users viewing this topic: none
Printable Version 

All Forums >> Web Development >> ASP and Database >> [PHP] Generic Form Validation Class?
Page: [1]
 
Ryokotsusai

 

Posts: 248
Joined: 10/5/2005
Status: offline

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

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)


_____________________________

The world is more like it is now than it ever has been before.
--Dwight Eisenhower
ou812

 

Posts: 1538
Joined: 1/5/2002
From: San Diego
Status: offline

 
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.

_____________________________

-brian

EnterpriseDB: Enterprise-class relational database management system
PostgreSQL: The world's most advanced open source database

(in reply to Ryokotsusai)
Ryokotsusai

 

Posts: 248
Joined: 10/5/2005
Status: offline

 
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...

< Message edited by Ryokotsusai -- 3/29/2008 10:34:24 >


_____________________________

The world is more like it is now than it ever has been before.
--Dwight Eisenhower

(in reply to ou812)
rdouglass

 

Posts: 9167
From: Biddeford, ME USA
Status: offline

 
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.

_____________________________

Don't take you're eye off your final destination.

ASP Checkbox Function Tutorial.

(in reply to Ryokotsusai)
Page:   [1]

All Forums >> Web Development >> ASP and Database >> [PHP] Generic Form Validation Class?
Page: [1]
Jump to: 1





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts