Beispiel #1
0
 /**
  * Sets the template Rule to use for actual validation
  *
  * We do not allow using Required rules here, they are able to validate
  * containers themselves without the help of Each rule.
  *
  * @param    HTML_QuickForm2_Rule    Template Rule
  * @return   HTML_QuickForm2_Rule
  * @throws   HTML_QuickForm2_InvalidArgumentException if $config is either not
  *               an instance of Rule or is an instance of Rule_Required
  */
 public function setConfig($config)
 {
     if (!$config instanceof HTML_QuickForm2_Rule) {
         throw new HTML_QuickForm2_InvalidArgumentException('Each Rule requires a template Rule to validate with, ' . preg_replace('/\\s+/', ' ', var_export($config, true)) . ' given');
     } elseif ($config instanceof HTML_QuickForm2_Rule_Required) {
         throw new HTML_QuickForm2_InvalidArgumentException('Cannot use "required" Rule as a template');
     }
     return parent::setConfig($config);
 }
Beispiel #2
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()));
 }
Beispiel #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)
  * @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()));
 }
Beispiel #4
0
 /**
  * Sets allowed MIME type(s) for the uploaded file
  *
  * @param    string|array    Allowed MIME type or an array of types
  * @return   HTML_QuickForm2_Rule
  * @throws   HTML_QuickForm2_InvalidArgumentException    if bogus configuration provided
  */
 public function setConfig($config)
 {
     if (0 == count($config) || !is_string($config) && !is_array($config)) {
         throw new HTML_QuickForm2_InvalidArgumentException('MimeType Rule requires MIME type(s), ' . preg_replace('/\\s+/', ' ', var_export($config, true)) . ' given');
     }
     return parent::setConfig($config);
 }
Beispiel #5
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);
 }
Beispiel #6
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));
 }
 /**
  * Sets maximum allowed file size
  *
  * @param    int     Maximum allowed size
  * @return   HTML_QuickForm2_Rule
  * @throws   HTML_QuickForm2_InvalidArgumentException    if a bogus size limit was provided
  */
 public function setConfig($config)
 {
     if (0 >= $config) {
         throw new HTML_QuickForm2_InvalidArgumentException('MaxFileSize Rule requires a positive size limit, ' . preg_replace('/\\s+/', ' ', var_export($config, true)) . ' given');
     }
     return parent::setConfig($config);
 }
 /**
  * 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);
 }