Example #1
0
 public function addRule(HTML_QuickForm2_Rule $rule, $triggers = false)
 {
     $id = $rule->getOwner()->getName();
     if ($id == '') {
         return;
     }
     list($ruleType, $ruleDef) = $this->translateRule($rule, $rule->getOwner());
     if (!$ruleType) {
         return;
     }
     $this->rules[$id][$ruleType] = $ruleDef;
     $this->messages[$id][$ruleType] = $rule->getMessage();
 }
Example #2
0
 /**
  * Sets the element that will be validated by this rule
  *
  * @param    HTML_QuickForm2_Element_InputFile   File upload field to validate
  * @throws   HTML_QuickForm2_InvalidArgumentException    if trying to use
  *           this Rule on something that isn't a file upload field
  */
 public function setOwner(HTML_QuickForm2_Node $owner)
 {
     if (!$owner instanceof HTML_QuickForm2_Element_InputFile) {
         throw new HTML_QuickForm2_InvalidArgumentException('MaxFileSize Rule can only validate file upload fields, ' . get_class($owner) . ' given');
     }
     parent::setOwner($owner);
 }
Example #3
0
 /**
  * Sets the callback to use for validation and its additional arguments
  *
  * @param    callback|array  Callback or array ('callback' => validation callback
  *                                              [, 'arguments' => additional arguments]
  *                                              [, 'js_callback' => javascript callback
  *                                                               for client-side validation])
  * @return   HTML_QuickForm2_Rule
  * @throws   HTML_QuickForm2_InvalidArgumentException if callback is missing or invalid
  *               or additional arguments is not an array
  */
 public function setConfig($config)
 {
     if (!is_array($config) || !isset($config['callback'])) {
         $config = array('callback' => $config);
     }
     if (!is_callable($config['callback'], false, $callbackName)) {
         throw new HTML_QuickForm2_InvalidArgumentException('Callback Rule requires a valid callback, \'' . $callbackName . '\' was given');
     }
     if (array_key_exists('arguments', $config) && !is_array($config['arguments'])) {
         throw new HTML_QuickForm2_InvalidArgumentException('Callback Rule expects additional callback arguments to be an array, ' . preg_replace('/\\s+/', ' ', var_export($config['arguments'], true)) . ' given');
     }
     return parent::setConfig($config + array('arguments' => array()));
 }
Example #4
0
 /**
  * Sets the element that will be validated by this rule
  *
  * @param    HTML_QuickForm2_Container   Container to validate
  * @throws   HTML_QuickForm2_InvalidArgumentException    if trying to use
  *           this Rule on something that isn't a Container
  */
 public function setOwner(HTML_QuickForm2_Node $owner)
 {
     if (!$owner instanceof HTML_QuickForm2_Container) {
         throw new HTML_QuickForm2_InvalidArgumentException('Each Rule can only validate Containers, ' . get_class($owner) . ' given');
     }
     parent::setOwner($owner);
 }
Example #5
0
 /**
  * Sets the callback to use for validation and its additional arguments
  *
  * @param    callback|array  Callback or array ('callback' => validation callback,
  *                                              'arguments' => additional arguments)
  * @return   HTML_QuickForm2_Rule
  * @throws   HTML_QuickForm2_InvalidArgumentException if callback is missing or invalid
  */
 public function setConfig($config)
 {
     if (!is_array($config) || !isset($config['callback'])) {
         $config = array('callback' => $config);
     }
     if (!is_callable($config['callback'], false, $callbackName)) {
         throw new HTML_QuickForm2_InvalidArgumentException('Callback Rule requires a valid callback, \'' . $callbackName . '\' was given');
     }
     return parent::setConfig($config + array('arguments' => array()));
 }
Example #6
0
 /**
  * Sets the allowed length limits
  *
  * $config can be either of the following
  *  - integer (rule checks for exact length)
  *  - array(minlength, maxlength)
  *  - array(['min' => minlength, ]['max' => maxlength])
  *
  * @param    int|array   Length limits
  * @return   HTML_QuickForm2_Rule
  * @throws   HTML_QuickForm2_InvalidArgumentException if bogus length limits
  *           were provided
  */
 public function setConfig($config)
 {
     if (is_array($config)) {
         $config = self::mergeMinMaxLength(array(), $config) + array('min' => 0, 'max' => 0);
     }
     if (is_array($config) && ($config['min'] < 0 || $config['max'] < 0) || !is_array($config) && $config < 0) {
         throw new HTML_QuickForm2_InvalidArgumentException('Length Rule requires limits to be nonnegative, ' . preg_replace('/\\s+/', ' ', var_export($config, true)) . ' given');
     } elseif (is_array($config) && $config['min'] == 0 && $config['max'] == 0 || !is_array($config) && 0 == $config) {
         throw new HTML_QuickForm2_InvalidArgumentException('Length Rule requires at least one non-zero limit, ' . preg_replace('/\\s+/', ' ', var_export($config, true)) . ' given');
     }
     if (!empty($config['min']) && !empty($config['max'])) {
         if ($config['min'] > $config['max']) {
             list($config['min'], $config['max']) = array($config['max'], $config['min']);
         } elseif ($config['min'] == $config['max']) {
             $config = $config['min'];
         }
     }
     return parent::setConfig($config);
 }
 /**
  * Adds a validation rule
  *
  * @param HTML_QuickForm2_Rule|string $rule           Validation rule or rule type
  * @param string|int                  $messageOrRunAt If first parameter is rule type,
  *            then message to display if validation fails, otherwise constant showing
  *            whether to perfom validation client-side and/or server-side
  * @param mixed                       $options        Configuration data for the rule
  * @param int                         $runAt          Whether to perfom validation
  *               server-side and/or client side. Combination of
  *               HTML_QuickForm2_Rule::SERVER and HTML_QuickForm2_Rule::CLIENT constants
  *
  * @return   HTML_QuickForm2_Rule            The added rule
  * @throws   HTML_QuickForm2_InvalidArgumentException    if $rule is of a
  *               wrong type or rule name isn't registered with Factory
  * @throws   HTML_QuickForm2_NotFoundException   if class for a given rule
  *               name cannot be found
  */
 public function addRule($rule, $messageOrRunAt = '', $options = null, $runAt = HTML_QuickForm2_Rule::SERVER)
 {
     if ($rule instanceof HTML_QuickForm2_Rule) {
         $rule->setOwner($this);
         $runAt = '' == $messageOrRunAt ? HTML_QuickForm2_Rule::SERVER : $messageOrRunAt;
     } elseif (is_string($rule)) {
         $rule = HTML_QuickForm2_Factory::createRule($rule, $this, $messageOrRunAt, $options);
     } else {
         throw new HTML_QuickForm2_InvalidArgumentException('addRule() expects either a rule type or ' . 'a HTML_QuickForm2_Rule instance');
     }
     $this->rules[] = array($rule, $runAt);
     return $rule;
 }
Example #8
0
 /**
  * Sets the regular expression to validate with
  *
  * @param    string  Regular expression
  * @return   HTML_QuickForm2_Rule
  * @throws   HTML_QuickForm2_InvalidArgumentException    if $config is not a string
  */
 public function setConfig($config)
 {
     if (!is_string($config)) {
         throw new HTML_QuickForm2_InvalidArgumentException('Regex Rule requires a regular expression, ' . preg_replace('/\\s+/', ' ', var_export($config, true)) . ' given');
     }
     return parent::setConfig($config);
 }
 /**
  * Sets minimum number of nonempty values
  *
  * This is useful for multiple selects and Containers, will be ignored for
  * all other elements. Defaults to 1, thus multiple select will be
  * considered not empty if at least one option is selected, Container will
  * be considered not empty if at least one contained element is not empty.
  *
  * @param int $config Minimum number of nonempty values
  *
  * @return   HTML_QuickForm2_Rule
  * @throws   HTML_QuickForm2_InvalidArgumentException    if a bogus limit was provided
  */
 public function setConfig($config)
 {
     if (is_null($config)) {
         $config = 1;
     } elseif (1 > intval($config)) {
         throw new HTML_QuickForm2_InvalidArgumentException('Nonempty Rule accepts a positive count of nonempty values, ' . preg_replace('/\\s+/', ' ', var_export($config, true)) . ' given');
     }
     return parent::setConfig(intval($config));
 }
 public function testDefaultConfigMerging()
 {
     $this->assertEquals('foo', HTML_QuickForm2_Rule::mergeConfig('foo', null));
     $this->assertEquals('bar', HTML_QuickForm2_Rule::mergeConfig('foo', 'bar'));
     HTML_QuickForm2_Factory::registerRule('no-config', 'HTML_QuickForm2_Rule_ImplConst');
     HTML_QuickForm2_Factory::registerRule('with-config', 'HTML_QuickForm2_Rule_ImplConst', null, 'bar');
     $el = new HTML_QuickForm2_Element_InputText();
     $this->assertEquals('foo', $el->createRule('no-config', '', 'foo')->getConfig());
     $this->assertEquals('bar', $el->createRule('with-config', '', 'foo')->getConfig());
 }
 /**
  * Adds the Rule javascript to the list of current form Rules
  *
  * @param HTML_QuickForm2_Rule
  */
 public function addRule(HTML_QuickForm2_Rule $rule)
 {
     $this->rules[$this->formId][] = $rule->getJavascript();
 }
 /**
  * Adds the Rule javascript to the list of current form Rules
  *
  * @param HTML_QuickForm2_Rule $rule     Rule instance
  * @param bool                 $triggers Whether rule code should contain
  *                                       "triggers" for live validation
  */
 public function addRule(HTML_QuickForm2_Rule $rule, $triggers = false)
 {
     $this->rules[$this->formId][] = $rule->getJavascript($triggers);
 }
 /**
  * Sets the comparison operator and operand to compare to
  *
  * $config can be either of the following
  *  - operand
  *  - array([operator, ]operand)
  *  - array(['operator' => operator, ]['operand' => operand])
  * If operator is missing it will default to '==='
  *
  * @param mixed $config Configuration data
  *
  * @return   HTML_QuickForm2_Rule
  * @throws   HTML_QuickForm2_InvalidArgumentException if a bogus comparison
  *           operator is used for configuration, if an operand is missing
  */
 public function setConfig($config)
 {
     if (0 == count($config)) {
         throw new HTML_QuickForm2_InvalidArgumentException('Compare Rule requires an argument to compare with');
     }
     $config = self::toCanonicalForm($config);
     $config += array('operator' => '===');
     if (!in_array($config['operator'], $this->operators)) {
         throw new HTML_QuickForm2_InvalidArgumentException('Compare Rule requires a valid comparison operator, ' . preg_replace('/\\s+/', ' ', var_export($config['operator'], true)) . ' given');
     }
     if (in_array($config['operator'], array('==', '!='))) {
         $config['operator'] .= '=';
     }
     return parent::setConfig($config);
 }