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!
blog comments powered by Disqus |
|
|
|
|
|
|
|