Login

Building a More Capable Validation Helper Class

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.

Quite often, helper classes are overlooked by developers taking their first steps in PHP-based web development. They are used to encapsulate all of the functionality required to perform certain tasks in full-featured, large-scale libraries, which sometimes contributes to breaking down the intrinsic modularity imposed by the object-oriented paradigm.

However, experienced programmers know that helper classes play an important role when it comes to speeding up the development of web applications, in typical situations where it’s necessary to dynamically generate different portions of a URL, validate incoming data or apply different formatting filters to a group of database records.

Given the relevance that helper classes have in the life of conscientous PHP developers, they deserve a closer analysis that must be complemented with a decent variety of examples that show how to build them and how to use them in concrete cases.

So, if you’re interested in learning the basic concepts that surround the creation of helpers, then this series of articles might provide the material for which you’re looking.

Logically, if you had the chance to read all of the tutorials that precede this one, then it’s quite probable that you now have a more solid background in building several helper classes with PHP 5. In those articles I offered numerous examples that hopefully demonstrated how to accomplish this process in a relatively easy way.

In the last chapter I started developing a simple helper class whose main task was to validate different types of data, such as integer and float numbers, and alphabetic and alphanumeric values, by using the capabilities provided by the PHP filters extension.

Admittedly, in its current state, this helper class has limited functionality. It would be really useful to add more methods to it to validate other things, like email and IP addresses, to mention just two examples. Thus, in the next few lines I’m going to extend the abilities of this helper class so it can be used to perform the validation processes mentioned above.

Now, it’s time to get rid of the preliminaries and keep digging deeper into the huge terrain of PHP helper classes. Let’s get going!

{mospagebreak title=Review: the ValidatorHelper class}

As usual, before I proceed to add more methods to the validation helper class developed in the previous part of the series, I will review how it was initially defined.

Below I included for you the complete signature of this sample class, so you can quickly recall how it looked. Here’s the class in question:

As illustrated above, the “ValidatorHelper” class relies heavily on some PHP filters to validate certain data types, such as float and integer numbers, as well as alphabetic and alphanumeric values. In each case, the method responsible for performing a specific verification simply will return FALSE or an empty string if the inputted parameter is considered invalid.

So far, so good, right? At this stage I’m sure that you’re familiar with the logic that drives the previous validation helper class, which implies that it’s time to continue expanding the class’s current functionality by adding to it a few more checking methods.

However, this process will be accomplished in the course of the following section. So click on the link that appears below and keep on reading.

Certainly, there’s plenty of room to extend the capacities of the validation helper. In its current state, the class is only capable of validating numbers and alphabetic and alphanumeric values, which isn’t enough even for a sample class like this.

So, to extend the functionality of the helper, I’m going to code three brand new methods, aimed at validating IP and email addresses and URLs as well, by taking advantage (again) of the capabilities given by the PHP filter extension.

That being explained, I suggest that you take a look at these methods, whose signatures have been listed below. Here they are:

As you can see, the three methods shown above are extremely easy to grasp; they behave like basic wrappers for some popular PHP filters. In the first case, as its name suggests, the “validate_url()” method is responsible for checking whether or not a supplied URL is valid, while in the second example, the “validate_ip()” method performs this same task with IP addresses.

Finally, the last method is tasked with checking email addresses at a basic level, by using the FILTER_VALIDATE_EMAIL filter. That was really easy to understand, right?

Of course, if you’re pretty familiar with working with PHP filters, then you’ll realize that the previous methods can be easily modified to perform a more strict validation process on each supplied parameter. But for the moment I’ll keep them this simple, so the class’s code remains uncluttered and more readable.

Well, at this point I provided the validation helper class with the ability to validate IP and email addresses, in addition to checking URLs. So it’s time to put all of the pieces together and list the helper’s full source code, including the three new methods that you just learned.

This process will be carried out in the last section of this tutorial. Please click on the link shown below and read the final segment.

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:

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!