HomePHP Page 2 - Building a PHP5 Form Processor: Coding the Form Validator Module
Checking for empty and integer values: looking at the “validateEmpty()” and “validateInteger()” methods - PHP
In this second part of a three-part series, we take up the topic of server-side validation. By the time you finish this article, you'll have the general guidelines for how to build a form validating class. You'll use some PHP built-in introspection functions, along with regular expressions, to assist you in building this class.
My round trip for defining the checking methods of the class begins with the specific implementation of the “validateEmpty()” method, which can be used to quickly check whether a form field is empty or not. Below is the pertinent method definition:
public function validateEmpty ($field,$errorMessage,$min=4,$max=32){ if(!isset($_POST[$field])||trim($_POST[$field])==''||strlen ($_POST[$field])<$min||strlen($_POST[$field])>$max){ $this->errors[]=$errorMessage; } }
While the above method performs a rather trivial checking process, such as validating empty fields, it’s worth examining in detail, since all the subsequent methods will stick to a similar approach.
As you’ve seen, this method is called with two arguments: the name of the form field to be tested, and the error message to be displayed in case the field is found to be empty. Besides the mandatory parameters, the method takes two additional arguments defined with default values, $min and $max. These arguments come in hand for checking whether values are within a given length of characters.
Now, by returning to the validation process itself, notice how the PHP $_POST array is used internally, in order to access form variables. In this case, I’ve purposely utilized this PHP proprietary array, simply because I’m assuming that the form will use this request method. However, if you’re going to build your forms by using a specific request method, you may want to either hard code the proper $_POST /$_GET array for accessing form variables, or (a much better approximation) write a class method for obtaining the method used on the form.
With reference to this method, if the field is empty, then the corresponding error message passed in as an argument is stored as a new element in the $errors array, so it can be accessed at a later time, after the form has been submitted.
Since all of the checking process is extremely easy to follow, and the mechanism for storing error messages consists of a simple array, I can move forward to explain another validation method. In particular, this one is responsible for verifying whether a field contains an integer value. Its signature is listed below:
public function validateInt($field,$errorMessage){ if(!isset($_POST[$field])||!is_numeric($_POST[$field]) ||intval($_POST[$field])!=$_POST[$field]){ $this->errors[]=$errorMessage; } }
As you can appreciate, the above method looks a little more interesting. In short, it will test whether the value entered in the field is an integer. In order to check for this condition, the method uses the powerful combination of the “is_numeric()” and “intval()” PHP functions respectively. As with the first method, if the value entered isn’t an integer, the appropriate error message is added as a new element to the $errors array. Simple and efficient.
Now that you have a general idea of how most of the class methods work, it’s time to move on and define a few more methods. These are useful for testing whether the data is within a given range of numeric values, and eventually whether it’s numeric or not. So, scroll down the page and keep reading.