|
|
|
 |
 |
 |
|
Description:
You can validate the value of a specific field at two ways.
- Use your own validator
- Use a build-in validator from FormHandler
Below is explained how to do each option.
1. Use your own validator
To use your own validator you have to write a function which checks the value of the field. The value of the field is passed to the function as argument.
After checking the value of the field you have to return if the value was valid or not.
- The value is valid if you return true.
- The value is invalid if you return false or a string. When you return a string, the text is used as error message.
After building the function you can give FormHandler the name of the function by the field you want to validate. You have to do this at the argument called "validator" (this argument is present in each field).
Example:
<?php
// include the class
include("FH3/class.FormHandler.php");
// create a new formhandler object
$form =& new FormHandler();
// textfield which we want to validate
// with our OWN function called myValidator
$form -> textField("Name", "name", "myValidator");
// submitbutton
$form -> submitButton();
// set the handler
$form -> onCorrect("doRun");
// flush it
$form -> flush();
// the handler function
function doRun( $data )
{
echo "Hello " . $data["name"];
}
// Our own validation function!!!!
function myValidator( $value )
{
// check the value
if( strLen( $value ) == 0)
{
return "You have to enter your name!";
}
// there is somehting subitted.. value is OK
else
{
return true;
}
}
?>
|
The result after submitting an empty field:

2. Use a build-in validator from FormHandler
FormHandler has several build-in functions to check if the value of a field is valid or not. When not, the default error message is shown (from the language file).
FormHandler has these build in functions:
- FH_STRING - Any string that doesn't have control characters (ASCII 0 - 31) but spaces are allowed
- FH_ALPHA - Only letters a-z and A-Z
- FH_DIGIT - Only numbers 0-9
- FH_ALPHA_NUM - Letters and numbers
- FH_INTEGER - Only numbers 0-9 and an optional - (minus) sign (in the beginning only)
- FH_FLOAT - Like FH_INTEGER, only with , (comma) or . (dot)
- FH_FILENAME - A valid file name (including dots but no slashes and other forbidden characters)
- FH_BOOL - A boolean (TRUE is either a case-insensitive "true" or "1". Everything else is FALSE)
- FH_VARIABLE - A valid variable name (letters, digits, underscore)
- FH_PASSWORD - A valid password (alphanumberic + some other characters but no spaces. Only allows ASCII 33 - 126)
- FH_URL - A valid URL
- FH_URL_HOST - A valid URL (http connection is used to check if url exists!)
- FH_EMAIL - A valid email address (only checks for valid format: xxx@xxx.xxx)
- FH_EMAIL_HOST - Like FH_EMAIL only with host check
- FH_TEXT - Like FH_STRING, but newline characters are allowed
- FH_NOT_EMPTY - Check if the value is not empty
- FH_NO_HTML - Check if the value does not contain any HTML
- FH_IP - Check if the value is an valid ip adres (xxx.xxx.xxx.xxx:xxxx). Port number is allowed)
- FH_POSTCODE - For dutch people!) A valid dutch postcode (eg. 9999 AA)
- FH_PHONE - For dutch people!) A valid dutch phone-number(eg. 058-2134778)
Note: When you want to allow an empty value but when the field is not empty it has to be valid you can use the same functions as above only beginning with a underscore ( _ ). Example: _FH_STRING
The functions above can be set at the same way you should set your own validator, only do not put quotes arround them!
Example: Usage of a build in validator
<?php
// include the class
include("FH3/class.FormHandler.php");
// create a new formhandler object
$form =& new FormHandler();
// textfield which we want to validate
// with the build in validator FH_STRING
$form -> textField("Name", "name", FH_STRING);
// submitbutton
$form -> submitButton();
// set the handler
$form -> onCorrect("doRun");
// flush it
$form -> flush();
// the handler function
function doRun( $data )
{
echo "Hello " . $data["name"];
}
?>
|
The result after submitting an empty field:
Latest change: 18 September 06 / 09:58
Comments
| Teye Heimans |
14 December 07 / 09:42 |
|
Be careful with using FH_TEXT in combination with large amount of data. It will result in a blank page (don't know exactly why).
See this note: http://www.formhandler.net/manual/32/Editor.html
| | Quote | | | | Note: When submitting very much data please dont use the FH_TEXT validator. The validator is to time/memory expensive and submitting will fail. For small and normal amounts of data you can just use FH_TEXT. |
| |
|
|
|
|