Home arrow PHP arrow Page 3 - Creating a Validation Helper Class

Creating a validation class - PHP

Helper classes seem easy to build -- almost deceptively so. If you're building a helper class to assist with a crucial function, however, such as validation, the approach you need to take may seem a bit less straightforward. This fifth article in an eight-part series on building helper classes walks you through the process, complete with hands-on examples.

TABLE OF CONTENTS:
  1. Creating a Validation Helper Class
  2. Review: a URL-handling helper class
  3. Creating a validation class
  4. Validating alphabetic and alphanumeric values
By: Alejandro Gervasio
Rating: starstarstarstarstar / 4
August 17, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

You, as a PHP developer, know that one of the most common and crucial tasks that a PHP application must perform is validating data, generally coming from users or visitors of a web site. In object-based programs, this process is usually delegated to a full-blown validation library, but there are cases where itís also possible to combine the functionality of a library like this with the capability provided by a simpler helper.

In this particular case, Iím going to demonstrate how to build a basic helper class that will validate inputted data, without boasting the complexity of a fully-loaded library.

Now that I have clarified that point, please take a look at the initial definition of this validation helper class, which looks like this:

class ValidatorHelper

{

// constructor not implemented

public function __construct(){}

 

// validate integer

public function validate_int($value, $min, $max)

{

return filter_var($value, FILTER_VALIDATE_INT, array('options' => array('min_range' => $min, 'max_range' => $max)));

}

 

// validate float number

public function validate_float($value)

{

return filter_var($value, FILTER_VALIDATE_FLOAT);

}

}

As shown above, the whole new validation helper class is comprised of only a couple of methods, apart from the constructor, for validating integers and float numbers respectively.

Also, itís worthwhile to notice that the pertinent validation tasks are accomplished by using the PHP filter extension. This makes for a truly no-brainer process, but itís also possible to replace the filters with custom methods that perform the same validation processes.

So far, everything looks pretty good. At this stage I've developed a validation helper class that verifies integers and float numbers. While this can be pretty useful as a starting point, itís possible to aggregate more functionality to the helper class by coding other methods that validate other data types.

Based on that premise, in the last section of this tutorial Iím going to add a pair of additional methods to the helper class, which will be charged with validating alphabetic and alphanumeric values.

To see how these methods will be defined, go ahead and read the following segment. Itís only one click away.



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: