previousHTML_QuickForm::addGroupRule() (Previous) (Next) HTML_QuickForm::isElementRequired()next

View this page in Last updated: Sun, 28 Jun 2009
English | Brazilian Portuguese | Chinese | Dutch | French | German | Hungarian | Japanese | Polish | Russian | Spanish | Turkish

HTML_QuickForm::addFormRule()

HTML_QuickForm::addFormRule() – Adds a global validation rule

Synopsis

require_once 'HTML/QuickForm.php';

void HTML_QuickForm::addFormRule ( mixed $rule )

Description

This should be used when you want to add a rule involving several fields or if you want to use some completely custom validation for your form. The rule function/method should return TRUE in case of successful validation and array('element name' => 'error') when there were errors.

Parameter

mixed $rule

A valid callback

Throws

Possible PEAR_Error values
Error code Error message Reason Solution
QUICKFORM_INVALID_RULE Callback function does not exist in HTML_QuickForm::addFormRule() Tried to pass a name of a non-existant function as a callback Check spelling

Note

since 3.1

This function can not be called statically.

Example

Using addFormRule()

<?php
require_once ('HTML/QuickForm.php');

$form = new HTML_QuickForm();

// the function checks whether the passwords are the same
function cmpPass($fields)
{
    if (
strlen($fields['passwd1']) && strlen($fields['passwd2']) &&
        
$fields['passwd1'] != $fields['passwd2']) {
        return array(
'passwd1' => 'Passwords are not the same');
    }
    return 
true;
}

$form->addElement('password''passwd1''Enter password');
$form->addElement('password''passwd2''Confirm password');

$form->addFormRule('cmpPass');
?>
previousHTML_QuickForm::addGroupRule() (Previous) (Next) HTML_QuickForm::isElementRequired()next

Download Documentation Last updated: Sun, 28 Jun 2009
Do you think that something on this page is wrong? Please file a bug report or add a note.
User Notes:
Note by: giuliano@seznam.cz
How do I get the error to show in the javascript?