Before I proceed to modify the pertinent declarations of the methods that comprise the validation helper class, it'd be helpful to reintroduce its complete source code. In this way you can recall how it was defined originally. Here's the entire signature of the helper. Pay close attention to 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); } } In the previous part of the series I offered a detailed explanation of how the above helper class was built, so I'm not going to waste your time discussing this topic here. Instead, I'm going to list the examples developed in that tutorial that show how to use it to validate different arguments. Here are the pertinent examples. Again, take a close look at them: $validator = new ValidatorHelper();
// example on validating an integer value if ($validator->validate_int(10.2, 1, 100) === FALSE) { echo 'Input value is not a valid integer.'; } else { echo 'Input value is a valid integer.'; } /* displays the following Input value is not a valid integer. */
// example on validating a float value if ($validator->validate_float(1234.5) === FALSE) { echo 'Input value is not a valid float number.'; } else { echo 'Input value is a valid float number.'; } /* displays the following Input value is a valid float number. */
// example on validating an alphabetic value if ($validator->validate_alpha('a1234') === FALSE) { echo 'Input value is not a valid alphabetic value.'; } else { echo 'Input value is a valid alphabetic value.'; } /* displays the following Input value is not a valid alphabetic value. */
// example on validating an alphanumeric value if ($validator->validate_alphanum('a1234') === FALSE) { echo 'Input value is not a valid alphanumeric value.'; } else { echo 'Input value is a valid alphanumeric value.'; } /* displays the following Input value is a valid alphanumeric value. */
// example on validating a URL if ($validator->validate_url('http://devshed.com') === FALSE) { echo 'Input value is not a valid URL.'; } else { echo 'Input value is a valid URL.'; } /* displays the following Input value is a valid URL. */
// example on validating an IP address if ($validator->validate_ip('127.0.0.1') === FALSE) { echo 'Input value is not a valid IP address.'; } else { echo 'Input value is a valid IP address.'; } /* displays the following Input value is a valid IP address */
// example on validate an email address if ($validator->validate_email('info@domain.com') === FALSE) { echo 'Input value is not a valid email address.'; } else { echo 'Input value is a valid email address.'; } /* displays the following Input value is a valid email address */ Undoubtedly, understanding how to use the "ValidatorHelper" class to verify IP and email addresses, alphabetic and alphanumeric values, and so forth is indeed a straightforward process that should be easy for you to tackle. Nevertheless, apart from studying the way that each validation process is properly performed, I'd like you to pay attention to the issue that I mentioned in the introduction, which certainly makes an inappropriate use of the helper. Yes, you guessed right! In all the cases the methods of the class have been called in the object scope, which is completely unnecessary for the reasons that I gave at the beginning of the article. So, to solve this issue quickly and elegantly, in the following segment I'm going to modify the definitions of the pertinent methods by declaring them static. It'll be that simple, really. To learn the full details of this process, click on the link below and read the section to come.
blog comments powered by Disqus |
|
|
|
|
|
|
|