/** * Validates the element's value * * @return bool whether uploaded file's MIME type is correct * @throws HTML_QuickForm2_InvalidArgumentException if a bogus $registeredType * was passed to constructor or bogus MIME type(s) provided */ protected function checkValue($value) { if (!empty($this->registeredType)) { $mime = HTML_QuickForm2_Factory::getRuleConfig($this->registeredType); } else { $mime = null; } if (null === $mime) { $mime = $this->getOptions(); } if (0 == count($mime) || !is_string($mime) && !is_array($mime)) { throw new HTML_QuickForm2_InvalidArgumentException('MimeType Rule requires MIME type(s), ' . preg_replace('/\\s+/', ' ', var_export($mime, true)) . ' given'); } if (!isset($value['error']) || UPLOAD_ERR_NO_FILE == $value['error']) { return true; } return is_array($mime) ? in_array($value['type'], $mime) : $value['type'] == $mime; }
/** * Validates the element's value * * @return bool whether uploaded file's size is within given limit * @throws HTML_QuickForm2_InvalidArgumentException if a bogus $registeredType * was passed to constructor or a bogus size limit was provided */ protected function checkValue($value) { if (!empty($this->registeredType)) { $limit = HTML_QuickForm2_Factory::getRuleConfig($this->registeredType); } else { $limit = null; } if (null === $limit) { $limit = $this->getOptions(); } if (0 >= $limit) { throw new HTML_QuickForm2_InvalidArgumentException('MaxFileSize Rule requires a positive size limit, ' . preg_replace('/\\s+/', ' ', var_export($limit, true)) . ' given'); } if (!isset($value['error']) || UPLOAD_ERR_NO_FILE == $value['error']) { return true; } return $limit >= @filesize($value['tmp_name']); }
/** * Validates the element's value * * @return bool whether element's value matches given regular expression * @throws HTML_QuickForm2_InvalidArgumentException if a bogus $registeredType * was passed to constructor * @throws HTML_QuickForm2_Exception if regular expression is missing */ protected function checkValue($value) { if (!empty($this->registeredType)) { $regex = HTML_QuickForm2_Factory::getRuleConfig($this->registeredType); } else { $regex = null; } if (null === $regex) { $regex = $this->getOptions(); } if (!is_string($regex)) { throw new HTML_QuickForm2_Exception('Regex Rule requires a regular expression, ' . preg_replace('/\\s+/', ' ', var_export($regex, true)) . ' given'); } if ($this->owner instanceof HTML_QuickForm2_Element_InputFile) { if (!isset($value['error']) || UPLOAD_ERR_NO_FILE == $value['error']) { return true; } $value = $value['name']; } elseif (!strlen($value)) { return true; } return preg_match($regex . 'D', $value); }
public function testGetRuleConfig() { HTML_QuickForm2_Factory::registerRule('foobar', 'FooBar', null, 'Some config'); $this->assertEquals('Some config', HTML_QuickForm2_Factory::getRuleConfig('foobar')); try { $config = HTML_QuickForm2_Factory::getRuleConfig('foobar_' . mt_rand()); } catch (HTML_QuickForm2_InvalidArgumentException $e) { $this->assertRegexp('/Rule(.*)is not known/', $e->getMessage()); return; } $this->fail('Expected HTML_QuickForm2_InvalidArgumentException was not thrown'); }
/** * Validates the element's value * * @return bool the value returned by a callback function * @throws HTML_QuickForm2_InvalidArgumentException if a bogus $registeredType * was passed to constructor or a bogus callback was provided * @throws HTML_QuickForm2_Exception if the callback is missing */ protected function checkValue($value) { if (!empty($this->registeredType)) { $config = HTML_QuickForm2_Factory::getRuleConfig($this->registeredType); } else { $config = null; } $callback = $this->findCallback($config); $arguments = $this->findArguments($config); if (!is_callable($callback, false, $callbackName)) { throw new HTML_QuickForm2_InvalidArgumentException('Callback Rule requires a valid callback, \'' . $callbackName . '\' was given'); } return call_user_func_array($callback, array_merge(array($value), $arguments)); }
/** * Validates the element's value * * @return bool whether length of the element's value is within allowed range * @throws HTML_QuickForm2_InvalidArgumentException if a bogus $registeredType * was passed to constructor or bogus allowed length(s) were used * for rule configuration * @throws HTML_QuickForm2_Exception if rule configuration is missing */ protected function checkValue($value) { if (!empty($this->registeredType)) { $config = HTML_QuickForm2_Factory::getRuleConfig($this->registeredType); } else { $config = null; } $allowedLength = $this->findAllowedLength($config); if (0 == ($valueLength = strlen($value))) { return true; } if (is_scalar($allowedLength)) { return $valueLength == $allowedLength; } else { return (!empty($allowedLength['min']) ? $valueLength >= $allowedLength['min'] : true) && (!empty($allowedLength['max']) ? $valueLength <= $allowedLength['max'] : true); } }
/** * Validates the element's value * * @return bool whether (element_value operator operand) expression is true * @throws HTML_QuickForm2_InvalidArgumentException if a bogus $registeredType * was passed to constructor or a bogus comparison operator is used * for configuration * @throws HTML_QuickForm2_Exception if an operand to compare with is missing */ protected function checkValue($value) { if (!empty($this->registeredType)) { $config = HTML_QuickForm2_Factory::getRuleConfig($this->registeredType); } else { $config = null; } $operator = $this->findOperator($config); $operand = $this->findOperand($config); if (!in_array($operator, array('===', '!=='))) { $compareFn = create_function('$a, $b', 'return floatval($a) ' . $operator . ' floatval($b);'); } else { $compareFn = create_function('$a, $b', 'return strval($a) ' . $operator . ' strval($b);'); } return $compareFn($value, $operand instanceof HTML_QuickForm2_Node ? $operand->getValue() : $operand); }