Home arrow PHP arrow Page 2 - A PHP Validation Helper Class in Action

Review: the previous ValidatorHelper class - PHP

If you’re an enthusiastic PHP developer interested in learning how to build helper classes, you've come to the right place. This article series will teach you in a few simple steps how to build several kinds of helper classes for manipulating strings, generating dynamic URLs and validate incoming data, and so forth.

  1. A PHP Validation Helper Class in Action
  2. Review: the previous ValidatorHelper class
  3. Checking integers and float numbers with the ValidatorHelper
  4. Validating alphabetic and alphanumeric values, IP and e-mail addresses, and URLs
By: Alejandro Gervasio
Rating: starstarstarstarstar / 3
August 31, 2009

print this article



Just in case you haven’t read the previous chapter of the series, where I finished building the validation helper class mentioned in the introduction, below I included the entire signature of this class, so you can see at a glance how it works.

Having said that, here’s how this sample helper was defined. Take a look at it, please:

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]+$/")));



// validate URL

public function validate_url($url)





// validate IP address

public function validate_ip($ip)


return filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);



// validate email address

public function validate_email($email)


return filter_var($email, FILTER_VALIDATE_EMAIL);



As illustrated above, practically all of the methods that compose the “ValidatorHelper” class (aside from the constructor, naturally) rely on the functionality provided by a particular PHP filter to validate a specific argument.

Quite possibly the weakest method of the helper is the one responsible for validating email addresses. The PHP filter that performs that task only checks the format of the address. However, it’s also possible to refactor this method and use other native PHP functions to check the existence of MX records in the DNS for the domain part of the email, but for the moment I’ll keep the implementation of this method relatively simple.

All in all, at this point you hopefully recalled how the “ValidatorHelper” class was defined initially. So, the next step that I’m going to take will consist of testing the functionality of the class through different examples that you’ll surely grasp in a snap.

Some of the examples will be developed in the course of the section to come. Therefore, to learn more, click on the link that appears below and read the next few lines.

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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


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