Source for file Boolean.php
Documentation is available at Boolean.php
* Copyright (c) 2006, Seth Price <{@link mailto:seth@pricepages.org seth@pricepages.org}> All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* - The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* @copyright Copyright (c) 2006, Seth Price
* @author Seth Price <seth@pricepages.org>
* @license http://opensource.org/licenses/bsd-license.php New BSD License
* @see SForm::__construct()
* Used to generate on-the-fly boolean rules
* This is a completely free form boolean statement, with an arbitrary number of
* elements used. It automatically uses the boolean statement to generate code
* for both client side and server side validation.
* 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.
* If the text input is blank <i>and</i> the value -2 is selected in the pull
* down menu, throw an error:
* $newNameElm->addRule( 'boolean', '%s == "" && %s == -2',
* array($newNameElm, $areaElm),
* 'If you are creating a new area, you must fill in it\'s name.' );
* Create a free form boolean rule
* The first arg is the messsage that will be returned when an error ocours.
* The second arg are the elements who's values will be fed into the boolean
* Last argument is a boolean string, with vars like {@link sprintf()} would
* accept. The placeholders in the string should be '%s' so we can insert
* the required text into it. The boolean string should evaluate to true iff
* @param string Error message
* @param array The elements used by the statement
* @param string Boolean statement
function __construct($message, $elements, $freeFormBool){
* Basic test intended to make sure that we aren't actually executing
* code in our "boolean statement".
if(strpos($freeFormBool, ';') !== false){
throw new Exception('It looks like you are trying to execute code in ' .
'your rule. This rule is intended to be limited to boolean ' .
throw new Exception('Your boolean statement must be in the form of a ' .
$elements = array($elements);
foreach($elements as $elm){
throw new Exception('The elements should be instances of ' .
* Does the statement evaluate to true?
* Values are found for each of the input elements, php code is created
* using those values, and the whole thing is fed through {@link eval()} to
* produce a boolean value.
* @return boolean Evaluation of the boolean statement
$vars[] = '"'. $elm->getValue(). '"';
* If anyone knows how to call a function with a variable number of
throw new Exception('eval() of the boolean statement returned a ' .
'non-null value. Please check it for validity.');
* Generates JS that will execute the boolean statement
* The code to locate the value of each element is created with the
* necessary local variables and the local variables are parsed into the
* boolean string. The strings are then inserted in the appropriate points
* in JS to throw an error if the boolean statement evaluated as true.
* @return string JavaScript
$id = $elm->getGlobalId();
$pre .= self::genJSValue($elm);
$varArgs[] = self::$jsElmVar[$id];
* If anyone knows how to call a function with a variable number of
$bool = call_user_func_array('sprintf', $varArgs);
|