Home arrow PHP arrow Page 2 - Building a More Capable Validation Helper Class

Review: the ValidatorHelper class - PHP

In this sixth part of an eight-part series on helper classes, I finish developing the validation helper class created in the previous part. We'll add even more useful validation methods to expand its functionality.

TABLE OF CONTENTS:
  1. Building a More Capable Validation Helper Class
  2. Review: the ValidatorHelper class
  3. Adding methods for validating IPs, email addresses and URLs
  4. The full source code for the improved validation helper class
By: Alejandro Gervasio
Rating: starstarstarstarstar / 4
August 24, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

As usual, before I proceed to add more methods to the validation helper class developed in the previous part of the series, I will review how it was initially defined.

Below I included for you the complete signature of this sample class, so you can quickly recall how it looked. Here’s the class in question:

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);

}

// validate alphabetic value

public function validate_alpha($value)

{

return filter_var($value, FILTER_VALIDATE_REGEXP, array('options' => array('regexp' => "/^[a-zA-Z]+$/")));

}

 

// validate alphanumeric value

public function validate_alphanum($value)

{

return filter_var($value, FILTER_VALIDATE_REGEXP, array('options' => array('regexp' => "/^[a-zA-Z0-9]+$/")));

}

}

As illustrated above, the “ValidatorHelper” class relies heavily on some PHP filters to validate certain data types, such as float and integer numbers, as well as alphabetic and alphanumeric values. In each case, the method responsible for performing a specific verification simply will return FALSE or an empty string if the inputted parameter is considered invalid.

So far, so good, right? At this stage I’m sure that you’re familiar with the logic that drives the previous validation helper class, which implies that it’s time to continue expanding the class’s current functionality by adding to it a few more checking methods.

However, this process will be accomplished in the course of the following section. So click on the link that appears below and keep on reading.



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

blog comments powered by Disqus
   

PHP ARTICLES

- 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...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 7 - Follow our Sitemap

Dev Shed Tutorial Topics: