Advanced Form Validation

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

User avatar
quocbao
Forum Commoner
Posts: 59
Joined: Sat Feb 04, 2006 2:03 am
Location: HCM,Vietnam
Contact:

Post by quocbao »

f5c31ba3f53a0b351f422cb01ea4dd2c

Well , i want to know what form is submited ( a page can contain many forms ) , so i generate this .
I'm doing a little change to the code to make it clearer :D
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

quocbao wrote:Well , i want to know what form is submited ( a page can contain many forms ) , so i generate this .
Yes, but the form designer also knows about the multiple forms and can give them more meaningful names.
(#10850)
User avatar
quocbao
Forum Commoner
Posts: 59
Joined: Sat Feb 04, 2006 2:03 am
Location: HCM,Vietnam
Contact:

Post by quocbao »

Well , this is the result . The designer can give him a name using form id
<form id="meaning_name" method=post action="somewhere" validation="client">
And a developer can use

Code: Select all

if (SmartValidator::validate($form,'meaning_name')) //is form meaning_name submitted and its data is valid
User avatar
quocbao
Forum Commoner
Posts: 59
Joined: Sat Feb 04, 2006 2:03 am
Location: HCM,Vietnam
Contact:

Post by quocbao »

And here is it http://diskc.net/smartValidator.rar

It's still a mess :( , don't try to apply it to your real application , because it can mess your HTML Id name Name ( i'm stupid ) , but you can test with it :)
I have written 4 rules so far , you can view it in "rule.txt" , the rule name is clear to understand :D

To enable validation for a form
<form ..... validation="mode">
where mode can be server,client,and both :)

Try it , and tell me what you think ..... about this mess :P
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

quocbao wrote:Try it , and tell me what you think ..... about this mess :P
Well I tried to try it. First, it would be great if you used gzip or zip rather than rar. Then use "<?php" in your files rather than "<?". And don't use pass by reference in smartValidator.php. And put "if (isset(...)) {" checks around your foreach loops.

Even after fixing all of that it didn't print any error messages. Maybe version 2?
(#10850)
User avatar
quocbao
Forum Commoner
Posts: 59
Joined: Sat Feb 04, 2006 2:03 am
Location: HCM,Vietnam
Contact:

Post by quocbao »

Here's the zip version : http://diskc.net/smartValidator.zip , :D sorry about rar extension
And don't use pass by reference in smartValidator.php

Code: Select all

function validate(&$data,$id=null,$break=false)
I want to pass by reference , because i want this function only return true / false .
If true then everything is ok , but if false then the form is not submited or data is not valid .
And put "if (isset(...)) {" checks around your foreach loops.
Can you show me this part ?
Even after fixing all of that it didn't print any error messages. Maybe version 2?
I try it on my server and it work :(
http://diskc.net/val/test.php

I'm re-coding it :( , let hope it will be better
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

It shouldn't be manipulating the data, I think that's the point arborint is trying to make.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Pass by reference example is this:

Code: Select all

$form_content = $smartValidator->parser_input($f , $form_content,&$server_validation);
Foreach loops should be checked like:

Code: Select all

if (isset($server_validation['name']))
				foreach ($server_validation['name'] as $name => $index)
				{
...
				}
			} else {
// set values for error here
			}
The code still needs normal "<?php" tags to be usable by others.

You might want to show all errors and notices and check the code.
(#10850)
User avatar
quocbao
Forum Commoner
Posts: 59
Joined: Sat Feb 04, 2006 2:03 am
Location: HCM,Vietnam
Contact:

Post by quocbao »

Pass by reference example is this:

Code: Select all

$form_content = $smartValidator->parser_input($f , $form_content,&$server_validation);
As you see , i use reference when a function return more than one value ( this function return new form content and server validation value ) . And i don't have any solution yet but pass by reference.
Foreach loops should be checked like:

Code: Select all

if (isset($server_validation['name']))
				foreach ($server_validation['name'] as $name => $index)
				{
...
				}
			} else {
// set values for error here
			}
:? , yes . I didn't notice that , if the form is empty then there will be some notice error :?
The code still needs normal "<?php" tags to be usable by others.
Sorry about that, i used "<?" when i first programmed PHP , and it's become a habit :oops: .
But why "<?php" ? "<?" is shorter :oops: :P
You might want to show all errors and notices and check the code.
I think you should put all error messages here.It's make easier to know what happen .
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

<?php vs <?

Answer: <?php is portable. Short tags are not.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

quocbao wrote:Pass by reference example is this:

Code: Select all

$form_content = $smartValidator->parser_input($f , $form_content,&$server_validation);
As you see , i use reference when a function return more than one value ( this function return new form content and server validation value ) . And i don't have any solution yet but pass by reference.
If you want to pass by reference then specify it in the function, not when you pass the variable to the function. The line above gives a Notice/Warning
quocbao wrote: Sorry about that, i used "<?" when i first programmed PHP , and it's become a habit :oops: .
But why "<?php" ? "<?" is shorter :oops: :P
I can't run the code with default PHP 5.1.2 settings, and had to change them all to '<?php'
(#10850)
User avatar
quocbao
Forum Commoner
Posts: 59
Joined: Sat Feb 04, 2006 2:03 am
Location: HCM,Vietnam
Contact:

Post by quocbao »

Thanks about <? vs <?php :)

I used to programing in PHP4 and PHP5.1 , so i didn't notice about that :)
Post Reply