Home arrow PHP arrow 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.

  1. Building a PHP5 Form Processor: Coding the Form Validator Module
  2. Checking for empty and integer values: looking at the “validateEmpty()” and “validateInteger()” methods
  3. Testing numeric values and ranges: defining the “validateNumber()” and “validateRange()” methods
  4. The big challenge: checking email addresses with the “validateEmail()” method
  5. Checking for errors: defining the “checkErrors()” and displayErrors()” methods
By: Alejandro Gervasio
Rating: starstarstarstarstar / 13
January 23, 2006

print this article



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

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){

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.

>>> More PHP Articles          >>> More By Alejandro Gervasio

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: