Building a PHP5 Form Processor: Coding the Form Validator Module - Checking for empty and integer values: looking at the “validateEmpty()” and “validateInteger()” methods (
Page 2 of 5 )
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.
| | Discuss Building a PHP5 Form Processor: Coding the Form Validator Module | | | | | | | The second article of this series walks through the development of the corresponding... | | | | | | Though in practical application you're not likely to run into this as often, a... | | | | | | First of all, I'd like to thank you for your comments on my article, and secondly... | | | | | | Hi Alejandro,
Thanks again for a great article. My compliments.
I have a... | | | | | | Hello Matthijs,
Thank you for your kind comments on this article. I simply used... | | | | | | hi,
thanks for this great article but will the third part be out..
eagerly... | | | | | | Isnt this... | | | | | | yep, that it. Thanks you for the link. | | | | | | Hi,
Thank you for your kind comments on this tutorial. Your feedback is really... | | | | | | Hey Rich, thanks for providing the correct link to the third article.
My Best... | | | | | | Hi,
great article(s),
but i have a question....the validateEmpty functions checks... | | | | | | Hi,
Thank you for commenting on my article. With regard to your question, if you... | | | | | | dont like the way this class relies on the global scope.. namely the _POST... | | | | | | Thank you for your suggestion. In fact, I mentioned this issue in the article too,... | | | | | | >>> Post your comment now! | | | | | |
|
 |