How can I make a piece field validation code a function?

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
edawson003
Forum Contributor
Posts: 133
Joined: Thu Aug 20, 2009 6:34 am
Location: Los Angeles, CA - USA

How can I make a piece field validation code a function?

Post by edawson003 »

I have applied the following validation code to a field that seems to work ok:

Code: Select all

  [color=#0000FF]if($_POST['[/color][color=#FF0000]field1[/color][color=#0000FF]'] == NULL){
   $reqerrors[] = "Yes";
   $cholesterolerror = "\t<br><span class=pred>You must enter a value.</span>\n";
        }    
   elseif(is_numeric($_POST['[/color][color=#FF0000]field1[/color][color=#0000FF]']) && $_POST['[/color][color=#FF0000]field1[/color][color=#0000FF]'] >= 0) {
   }
   else {
   $reqverrors[] = "Yes";
   $[/color][color=#FF0000]field1[/color][color=#0000FF]= "\t<br><span class=pred>Invalid value entered.</span>\n";
   }[/color]
Next to the form field, I include <? echo $field1error; ?>



Problem is, I have about 20 more fields that I would like to apply the above validation code. For maintanability sake, there has to be a slick way to make the above in blue a function, that I can apply like so:

function(field1)
function(field2)
function(field3)
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: How can I make a piece field validation code a function?

Post by alex.barylski »

You could easily iterate the array of POST values, but iMHO thats not a great practice. Validation is (and should be) more specific than that. You should check the minimum/maxium length of text fields, range of numeric fields, format of alpha numeric fields (ie: phone). When you follow this best practice, it becomes difficult (if not impossible) to apply a generic catch all validation routine.

If you find this tedious, you would probably benefit from using a Forms API, perhaps check out Zend framework: http://framework.zend.com/manual/en/zend.form.html

Cheers,
Alex
User avatar
edawson003
Forum Contributor
Posts: 133
Joined: Thu Aug 20, 2009 6:34 am
Location: Los Angeles, CA - USA

Re: How can I make a piece field validation code a function?

Post by edawson003 »

Thanks for your response.

I definiely understand your argument about the best practices approach to field validation; however, I want to apply this specific function to a batch of fields that are the same field type which is numeric, allow 0s, allow decimals and block NULLs. Could you please elaborate a bit on the "iterate the array of POST values" approach? How would I code it? Sorry, I'm a newb -- english please. :oops: In the meantime, I will try to brush up on how arrays work, because admittedly, I am a bit at loss with that syntax.

Also, thanks for Zend framework plug. I have heard about it a few times....I'll check it out.
User avatar
edawson003
Forum Contributor
Posts: 133
Joined: Thu Aug 20, 2009 6:34 am
Location: Los Angeles, CA - USA

Re: How can I make a piece field validation code a function?

Post by edawson003 »

I am thinking something along the lines of

Code: Select all

function nutrientval([color=#FF0000]$x[/color]) {
 
if($_POST['[color=#FF0000]$x[/color]'] == NULL){
   $reqerrors[] = "Yes";
   $[color=#FF0000]$x[/color] = "\t<br><span class=pred>You must enter a value.</span>\n";
        }    
   elseif(is_numeric($_POST['$x']) && $_POST['[color=#FF0000]$x[/color]'] >= 0) {
   }
   else {
   $reqverrors[] = "Yes";
   [color=#0000FF]$[/color][color=#FF0000]$x[/color] = "\t<br><span class=pred>Invalid value entered.</span>\n";
   }
   
   }
   
   ?>
and I would execute it like so nutrientval(fieldname);

...or something like that. Don't even know if this is possible.
Post Reply