Class: SForm_Rule
Source Location: /SForm.php
Class SForm_Rule
Class Overview
|
Create rules for elements
The SForm rules are very different than the QuickForm rules. Their advantages over QuickForm rules are twofold: They are designed to easily validate any number of elements simultaneously. They are also designed to throw an error (if needed) at any location in the form. This means that you can easily create complex rules, such as validating a credit card number depnding on which type of credit card is selected. You can require a user to fill in identifying information if they don't have a user name. You can better control filling out billing vs. shipping information. You can also do any of this in JavaScript. Much of this can even be done without defining a new class by using the SForm_Rule_Boolean to create an on-the-fly server and client side boolean statement. Usage: Because of the changes, the API is different. For example, this code from the original quick start guide: $form->addElement('text', 'name', 'Enter your name:',
array('size' => 50, 'maxlength' => 255));
$form->addRule('name', 'Please enter your name', 'required', null,'client');
Can be changed into this, for the same functionality: $form->addElement('text', 'name', 'Enter your name:',
array('size' => 50, 'maxlength' => 255));
$form->addRuleDep('name', 'Please enter your name', 'required', null,'client');
Note that in the example, addRule() has been changed to addRuleDep(), where 'Dep' stands for depriciated. The correct SForm usage, though, is (shorthand): $nameElm = $form->addElement('text', 'name', 'Enter your name:',
array('size' => 50, 'maxlength' => 255));
$nameElm->addRule('required', 'Please enter your name');
Or even (fully object oriented):
array('size' => 50, 'maxlength' => 255));
$nameElm->addRule($reqRule);
$form->addElement($nameElm);
Located in /SForm.php [line 2282]
Author(s):
API Tags:
|
Properties
|
Methods
|
Property Summary
| static integer |
$jsElmNum |
The counter for temp local variables |
| static array |
$jsElmVar |
The names of local JavaScript variables |
| array |
$elements |
Which elements fall under this jurisdiction? |
| boolean |
$hasParent |
Has this rule been asigned to an element? |
Method Summary
| static
string
|
escClient() |
Escape client side variables |
| static
string
|
genJSValue() |
Returns JS to get a value and save it in the local variable |
| SForm_Rule |
__construct() |
Construct a rule with one element and one error message |
| string |
createJSRule() |
Create a basic if-then rule in JavaScript |
| void |
isError() |
Returns true if this element(s) fail validation |
| string |
toJavaScript() |
Get the JavaScript that should be run onsubmit |
| string |
toString() |
Returns a message describing the error and its cause |
Properties
The counter for temp local variables
API Tags:
The names of local JavaScript variables
To save cycles and bytes, values are stored in local JavaScript variables before they are used in client side validation. This array is indexed by the Global ID, and the values are the names of the local variables. API Tags:
Which elements fall under this jurisdiction?
API Tags:
Has this rule been asigned to an element?
API Tags:
NO YUO!
The message that is returned when an error is thrown. toHtml() can be overridden and the message made more dynamic, if desired. API Tags:
Should we use this rule with JavaScript?
API Tags:
Methods
static string escClient(
string
$str
)
|
|
Escape client side variables
Use this to escape client side JavaScript text.
Parameters:
|
string |
$str: |
Text to escape |
API Tags:
| Return: | Escaped text |
| Access: | protected |
Returns JS to get a value and save it in the local variable
Generate JavaScript code that creates a temperarory local variable. The name of the variable is stored in $jsElmVar, and can be used in by other functions as needed. NOTE: This must be called for an element before the variable name in $jsElmVar can be used. Otherwise, there will be no entry in the array. This will save some browser cycles when we don't need to index into the array for each usage. It will also save some bytes in the script so the same huge ID strings aren't repeated. This will generate different code depending on the type of element used, some types of elements make it difficult to find their value in JavaScript.
Parameters:
|
object The |
$elm: |
object to get a value for |
API Tags:
| Return: | JavaScript code that saves the value in a local variable |
| Access: | protected |
| Uses: | SForm_Rule::$jsElmVar |
SForm_Rule __construct(
string
$message, SForm_Element
$element
)
|
|
Construct a rule with one element and one error message
The first argument is always the error message, and the second argument is always an element or array of elements (as appropriate). The elements passed here are not necessarily the element that validates this rule and throws an error message. This way, the rule can validate two or more fields in a group, but the error appears in the group. This rule will later be added to said element or container with SForm_Element::addRule(). By default, the message is static, but you are encouraged to create dynamic messages that address the specific error and element via SForm_Element::getValue() and SForm_Element::getLabel() respectively. Make sure, though, that messages still work on both the server side and client side. The default usage of this rule only applies to one element, but it is designed to apply to any number of elements.
Parameters:
|
string |
$message: |
Default error message |
|
object Element |
$element: |
to validate |
Redefined in descendants as:
Mark this rule as having a parent
Each rule should be applied to one and only one element. This is done so we can evaluate each rule once and only once. This also applies for rendering JavaScript and generating error messages. Each rule can validate more than one element, but that is handled within the rule, not within the element.
Parameters:
|
object Parent |
$elm: |
of this rule |
API Tags:
string createJSRule(
string
$jsPrefix, string
$jsTest
)
|
|
Create a basic if-then rule in JavaScript
This serves as glue for most JavaScript rules. It should be fed a prefix that sets up a test, and the boolean test itself. If the test is found to be true, an error message is created an the offending element is flagged.
Parameters:
|
string |
$jsPrefix: |
JS code to be run before the test |
|
string |
$jsTest: |
JS code that evaluates to true on error |
API Tags:
| Return: | Full JS test |
| Access: | protected |
void isError(
boolean
0
)
|
|
Returns true if this element(s) fail validation
Parameters:
|
boolean |
0: |
Are the element values valid? |
API Tags:
Redefined in descendants as:
Get the JavaScript that should be run onsubmit
This can be anything, but is generally a validation rule set up as an if- then statement. This code should be called by the Renderer, and is typically not directly accessed by users. First a prefix is computed that sets up any needed calculations. A common example of this is retrieving the value of the element that you wish to test. If you are coding a new rule, take a look at genJSValue() because it will save the value into a local variable for you. The name of the generated variable is stored in $jsElmVar, and is indexed by the Global ID of the element. The next part is the boolean test. Normally, an error is thrown if the test is true. A simple example would be "_name == ''". This detects whether there is a value stored in an element. The last part of the rule is flagging the error in the JavaScript. This should be as simple as appending the error message and flagging the error in errFlag[]. This part is done for you in createJSRule().
API Tags:
Redefined in descendants as:
Returns a message describing the error and its cause
I'd encourage you to customize this function to explicitly state what element is causing the problem and how to remove it. Make sure that the error message isn't too long, though.
API Tags:
| Return: | An error message |
| Access: | public |
void validateOnClient(
$bool
)
|
|
Should we run validation code on the client
This will enable or disable running JavaScript validation code on the client for this rule. By default, validation code is run. Rules that generate quite a bit of code or require a server connection (like a database) should be run only on the server.
Parameters:
API Tags:
|
|