A PHP Validation Helper Class in Action

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.

Welcome to the penultimate tutorial of the series on building helpers in PHP 5. Comprised of eight articles, this series provides you with the basic pointers to start creating your own helper classes with minor hassles, and complements the corresponding theory with a great variety of hands-on examples.

And now that you’ve been introduced to the aim of this series of articles, it’s time to review the topics that were treated in the last part. In that installment I finished building a simple helper class that could validate different types of data, including numeric and float numbers, IP and email addresses, and URLs as well.

The functionality of each method of this class was based mostly on PHP filters, since this extension not only permits you to perform a decent validation on incoming data, but it makes this a no-brainer process.

However, if you’re anything like me, then you may want to see how this validation helper class can be used in concrete situations, right? Well, in this tutorial I’m going to develop some basic examples that will illustrate the real capabilities of the class for verifying all the types of data mentioned above.

So, now it’s time to leave the preliminaries behind us and start evaluating this sample helper class through some concrete and easy-to-grasp examples. Let’s jump in!

{mospagebreak title=Review: the previous ValidatorHelper class}

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.

{mospagebreak title=Checking integers and float numbers with the ValidatorHelper}

In accordance with the concepts deployed in the section that you just read, it’s necessary to create some simple examples that show how to use the validation helper class in concrete cases. With that thought in mind, below I included two approachable code samples that use the helper for validating first an integer, and then a float number.

Here are the examples in question:

$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.

*/

Definitely, understanding the examples shown above is a pretty straightforward process. The “validate_int()” and “validate_float()” methods of the helper are called after creating an instance of it. Also, in this particular case I fed these methods with rather trivial values to make the examples easier to grasp, but logically more complex parameters can be used.

So far, so good, right? Now that you’ve grasped how to use the previous validation helper class for checking integer and float numbers, it’s time to continue evaluating its functionality. As you’ll recall, the class also has some other methods, which can be used for validating IP and email addresses, alphabetic and alphanumeric values and finally URLs.

So, in the last segment of this tutorial I’m going to demonstrate how to validate those specific types of data. So click on the link shown below and read the following lines. 

{mospagebreak title=Validating alphabetic and alphanumeric values, IP and e-mail addresses, and URLs}

To finish showing you how to use the validation helper class, the last thing that I’m going to do in this tutorial will be coding some additional examples that demonstrate how to use the class’s remaining methods for checking alphabetic and alphanumeric values, IP and email addresses, and URLs as well.

That being clarified, here’s the set of code samples that perform the aforementioned validation tasks:

$validator = new ValidatorHelper();

 

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

*/

As you can see above, validating email and IP addresses, alphabetic values and the like by using the interface provided by the helper class is a simple process that doesn’t offer major difficulties. The whole procedure is reduced to calling dynamically the appropriate method and nothing else. It’s that easy.

Finally, before you move on and read the conclusion, I’d like to point out again that all of the methods that make up this helper class can be largely improved, to perform a more strict validation on a given parameter.

I cited before the case of the “validate_email()” method, which certainly can be completely refactored by using other PHP functions, but this same concept may be applied to other methods of the class as well. So, if you’re bored and want to test your PHP skills, then try to create your own enhanced version of the previous validation helper.

Final thoughts

That’s all for now. In this seventh episode of the series, I demonstrated with a decent number of practical examples how to use this sample validation helper to verify integer and float numbers, IP and email addresses and so forth. As you saw previously, this process was very easy to follow, even if you have an average background in object-oriented programming with PHP 5.

And speaking of OOP, a good coding habit of this paradigm is to avoid unnecessary instantiation of classes. You can apply this principle to this specific case, since there’s no need to deal with a validation helper object when it’s possible to call its methods statically.

Demonstrating how to accomplish this will be the subject of the last installment of the series, so now you don’t have any excuses to miss it!

[gp-comments width="770" linklove="off" ]

chat sex hikayeleri Ensest hikaye