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) { 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); } } 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.
blog comments powered by Disqus |
|
|
|
|
|
|
|