Source for file ValidationUtil.class.php
Documentation is available at ValidationUtil.class.php
* Zikula Application Framework
* @copyright Robert Gasch
* @link http://www.zikula.org
* @version $Id: ValidationUtil.class.php 24342 2008-06-06 12:03:14Z markwest $
* @license GNU/GPL - http://www.gnu.org/copyleft/gpl.html
* @author Robert Gasch rgasch@gmail.com
* @subpackage ValidationUtil
* Validate a specific field using the supplied control parameters
* @param objectType The string object type
* @param object The object to validate
* @param field The field to validate
* @param required whether or not the field is required
* @param cmp_op The compare operation to perform
* @param cmp_value The value to compare the supplied field value to. If the value starts with a ':', the argument is used as an object access key.
* @param err_msg The error message to use if the validation fails
* @return A true/false value indicating whether the field validation passed or failed
function validateField ($objectType, $object, $field, $required, $cmp_op, $cmp_value, $err_msg)
return pn_exit ('ValidationUtil::validateField: object is not an array ... ');
return pn_exit ('ValidationUtil::validateField: empty field supplied ... ');
return pn_exit ('ValidationUtil::validateField: empty error message supplied ... ');
// if this field already has an error, don't perform further checks
if (isset ($_SESSION['validationErrors'][$objectType][$field])) {
if (!isset ($object[$field]) || $object[$field] === '' || $object[$field] === '0') {
if ($rc && $object[$field])
$postval = $object[$field];
if (substr($testval, 0, 1) == ':') // denotes an object access key
//print "$postval $cmp_op $testval";
$rc = ($postval === $testval);
$rc = ($postval != $testval);
$rc = ($postval !== '' && is_numeric($postval) && $postval > $testval);
$rc = ($postval !== '' && is_numeric($postval) && $postval >= $testval);
$rc = ($postval !== '' && is_numeric($postval) && $postval < $testval);
$rc = ($postval !== '' && is_numeric($postval) && $postval <= $testval);
if ($cmp_op == 'email') {
if ($cmp_op == 'valuearray') {
return pn_exit ('ValidationUtil::validateField: Testval for test valuearray is not an array ... ');
if ($cmp_op == 'noop' || !$cmp_op) {
return pn_exit ("ValidationUtil::validateField: invalid cmp_op [$cmp_op] supplied for non-required field [$field] ... ");
return pn_exit ("ValidationUtil::validateField: invalid cmp_op [$cmp_op] supplied for field [$field] ... ");
if (!isset ($_SESSION['validationErrors'][$objectType][$field])) {
$_SESSION['validationErrors'][$objectType][$field] = $err_msg;
* Validate a specific field using the supplied control parameters
* @param object The object to validate
* @param validationControl The structured validation control array
* The expected structure for the validation array is as follows:
* $validationControl[] = array ('field' => $fieldname,
* 'required' => true/false,
* 'cmp_op' => eq/neq/lt/lte/gt/gte/url/email/valuearray/noop,
* 'err_msg' => $errorMessage);
* The noop value for the cmp_op field is only valid if the field is not required
* @return A true/false value indicating whether the field validation passed or failed
$objType = $validationControl['objectType'];
$field = $validationControl['field'];
$req = $validationControl['required'];
$cmp_op = $validationControl['cmp_op'];
$cmp_val = $validationControl['cmp_value'];
$err_msg = $validationControl['err_msg'];
* Validate a specific field using the supplied control parameters
* @param objectType The string object type
* @param object The object to validate
* @param validationControls The array of structured validation control arrays
* The expected structure for the validation array is as follows:
* $validationControls[] = array ('field' => $fieldname,
* 'required' => true/false,
* 'cmp_op' => eq/neq/lt/lte/gt/gte/noop,
* 'err_msg' => $errorMessage), ...);
* The noop value for the cmp_op field is only valid if the field is not required
* @return A true/false value indicating whether the object validation passed or resulted in errors.
foreach ($validationControls as $vc)
$_SESSION['validationFailedObjects'][$objectType] = $object;
* Validate a specific field using the supplied plain validation array. This function converts
* the plain validation array into a structured validation array and then calls ValidationUtil::validateObject().
* @param objectType The string object type
* @param object The object to validate
* @param validationArray The plain (numerically indexed) validation array
* The expected structure for the validation array is as follows:
* $validationArray[] = array ($fieldname, true/false, eq/neq/lt/lte/gt/gte/noop, $value, $errorMessage);
* The noop value for the cmp_op field is only valid if the field is not required
* @return A true/false value indicating whether the object validation passed or failed
$validationControls = array ();
foreach ($validationArray as $va) {
return pn_exit ("ValidationUtil::validateObjectPlain : invalid validationArray supplied: expected 5 fields but found $size ... ");
$vc['objectType'] = $objectType;
$vc['required'] = $va[1];
$vc['cmp_value'] = $va[3];
$validationControls[] = $vc;
|