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

The full source code for the improved validation helper 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

If youíre anything like me, you want to see how the validation helper class looks after incorporating the three additional methods that were discussed in the previous section. Below I included the complete source code for the helper, so you can examine it in its totality. Here it is:

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)

{

return filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED);

}

 

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

}

}

Definitely, now that the ďValidatorHelperĒ class is capable of validating email and IP addresses in a pretty effective manner, it looks much more functional. Naturally, as I stated previously, itís easy to add more methods to the class, if you need to validate more specific inputs, such as phone and credit card numbers, ZIP codes, and so forth.

But this will be left as homework for you, as an exercise aimed at acquiring more practice in developing helper classes with PHP 5. Youíll have a great time doing it, thatís already guaranteed!

Final thoughts

Itís sad to say it, but thatís all for the moment. In this sixth episode of the series, I finished developing the previous validation helper class. Again, itís valid to point out that the class can be largely improved either by using other PHP filters or by defining additional custom methods that validate strings, phone and credit card numbers and so forth (feel free to add your own item to the list).

Considering that this helper is only an example, it does a pretty decent job of validating different types of data. Now, moving forward, itís time to talk about the topics that will be covered in the upcoming tutorial.

Since the development of the helper class is already complete, in the next article Iím going to set up some examples aimed at illustrating how to use it in some concrete situations.

So, now that youíve been warned about the subject of the forthcoming part of the series, you donít have any excuses to miss it!



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