/** * @param \HTML_QuickForm2_Node $element * * @return \HTML_QuickForm2_Node */ function initElement(\HTML_QuickForm2_Node $element) { \Debugbar::addMessage(($this->namespace ? $this->namespace . '::' : '') . 'models/' . $this->model->getEntity() . '.' . $element->getName()); $config = \Config::get(($this->namespace ? $this->namespace . '::' : '') . 'models/' . $this->model->getEntity() . '.' . $element->getName()); if (is_array($config)) { foreach ($config as $k => $v) { $method = \Str::camel('set_' . $k); if (is_callable([$element, $method])) { call_user_func([$element, $method], $v); } } } return $element; }
function ruleApply(\HTML_QuickForm2_Node &$el, $qf_rule, $laravel_rule, $params = null, $replaces = []) { switch (true) { case isset($this->validator_messages[$el->getNameDot() . '.' . $laravel_rule]): $message = $this->validator_messages[$el->getNameDot() . '.' . $laravel_rule]; break; case isset($this->validator_messages[$laravel_rule]): $message = $this->validator_messages[$laravel_rule]; break; default: $message = \Lang::get('validation.' . $laravel_rule); break; } $message = str_replace(':attribute', '"' . $el->getLabel() . '"', $message); $message = str_replace(array_keys($replaces), array_values($replaces), $message); $el->addRule($qf_rule, $message, $params, \HTML_QuickForm2_Rule::ONBLUR_CLIENT); }
/** * Returns the client-side representation of the Rule * * The Javascript object returned contains the following fields: * - callback: {@see getJavascriptCallback()} * - elementId: element ID to set error for if validation fails * - errorMessage: error message to set if validation fails * - chained: chained rules, array of arrays like in $chainedRules property * * @return string * @throws HTML_QuickForm2_Exception if Rule or its chained Rules can only * be run server-side */ public function getJavascript() { $js = "{\n\tcallback: " . $this->getJavascriptCallback() . ",\n" . "\telementId: '" . $this->owner->getId() . "',\n" . "\terrorMessage: '" . strtr($this->getMessage(), array("\r" => '\\r', "\n" => '\\n', "\t" => '\\t', "'" => "\\'", '"' => '\\"', '\\' => '\\\\')) . "',\n\tchained: ["; $chained = array(); foreach ($this->chainedRules as $item) { $multipliers = array(); foreach ($item as $multiplier) { $multipliers[] = $multiplier->getJavascript(); } $chained[] = '[' . implode(",\n", $multipliers) . ']'; } $js .= implode(",\n", $chained) . "]\n}"; return $js; }
/** * Returns the client-side representation of the Rule * * The Javascript object returned contains the following fields: * - callback: {@see getJavascriptCallback()} * - elementId: element ID to set error for if validation fails * - errorMessage: error message to set if validation fails * - chained: chained rules, array of arrays like in $chainedRules property * * @return string * @throws HTML_QuickForm2_Exception if Rule or its chained Rules can only * be run server-side */ public function getJavascript() { HTML_QuickForm2_Loader::loadClass('HTML_QuickForm2_JavascriptBuilder'); $js = "{\n\tcallback: " . $this->getJavascriptCallback() . ",\n" . "\towner: '" . $this->owner->getId() . "',\n" . "\tmessage: " . HTML_QuickForm2_JavascriptBuilder::encode($this->getMessage()); if (count($this->chainedRules) > 1 || count($this->chainedRules[0]) > 0) { $chained = array(); foreach ($this->chainedRules as $item) { $multipliers = array(); foreach ($item as $multiplier) { $multipliers[] = $multiplier->getJavascript(); } $chained[] = '[' . implode(",\n", $multipliers) . ']'; } $js .= ",\n\tchained: [" . implode(",\n", $chained) . "]"; } return $js . "\n}"; }
/** * Performs validation * * The whole rule chain is executed. Note that the side effect of this * method is setting the error message on element if validation fails * * @return boolean Whether the element is valid */ public function validate() { $globalValid = false; $localValid = $this->checkValue($this->owner->getValue()); foreach ($this->chainedRules as $item) { foreach ($item as $multiplier) { if (!$localValid) { break; } $localValid = $localValid && $multiplier->validate(); } $globalValid = $globalValid || $localValid; if ($globalValid) { break; } $localValid = true; } if (!$globalValid && strlen($this->message) && !$this->owner->getError()) { $this->owner->setError($this->message); } return $globalValid; }
/** * Renders a hidden element * * @param HTML_QuickForm2_Node $element Hidden element being rendered */ public function renderHidden(HTML_QuickForm2_Node $element) { if ($this->options['group_hiddens']) { $this->hidden[] = $element->__toString(); } }
public function renderHidden(HTML_QuickForm2_Node $element) { if ($this->options['group_hiddens']) { $this->array['hidden'][] = $element->__toString(); } else { $this->renderElement($element); } }
/** * Processes the element's template, adding label(s), required note and error message * * @param string Element template * @param HTML_QuickForm2_Node Element being rendered * @return string Template with some substitutions done */ public function prepareTemplate($elTpl, HTML_QuickForm2_Node $element) { // if element is required $elTpl = $this->markRequired($elTpl, $element->isRequired()); $elTpl = $this->outputError($elTpl, $element->getError()); return $this->outputLabel($elTpl, $element->getLabel()); }
function addValidateFunction(HTML_QuickForm2_Node $el) { foreach ((array) $this->validateFunc as $f) { switch ($f) { case 'required': $el->addRule('required'); break; case 'integer': $el->addRule('regex', ___("Integer value required"), '/^\\d+$/'); break; case 'number': $el->addRule('regex', ___("Numeric value required"), '/^\\d+(|\\.\\d+)$/'); break; case 'email': $el->addRule('callback', ___("Please enter a valid e-mail address"), array('Am_Validate', 'empty_or_email')); break; default: if (is_callable($f)) { $el->addRule('callback2', '--error--', $f); } break; } } }
/** * Sets the error message on the owner element */ protected function setOwnerError() { if (strlen($this->getMessage()) && !$this->owner->getError()) { $this->owner->setError($this->getMessage()); } }
/** * Applies recursive and non-recursive filters on element value * * @param mixed $value Element value * * @return mixed Filtered value */ protected function applyFilters($value) { $recursive = $this->recursiveFilters; $container = $this->getContainer(); while (!empty($container)) { $recursive = array_merge($container->recursiveFilters, $recursive); $container = $container->getContainer(); } foreach ($recursive as $filter) { if (is_array($value)) { array_walk_recursive($value, array('HTML_QuickForm2_Node', 'applyFilter'), $filter); } else { self::applyFilter($value, null, $filter); } } return parent::applyFilters($value); }
function findHelpUrl(HTML_QuickForm2_Node $el) { /// find help id $data = $el->getData(); if (!empty($data['help-path']) || !empty($data['help-id'])) { $url = ""; do { $data = $el->getData(); if (!empty($data['help-path'])) { $url = $data['help-path'] . $url; break; } elseif (!empty($data['help-id'])) { $url = $data['help-id'] . $url; } } while ($el = $el->getContainer()); $url = 'http://v4.amember.com/docs/' . $url; return $url; } }
public function validate() { return parent::validate(); }
/** * Returns the client-side representation of the Rule * * This creates an instance of either qf.Rule or qf.LiveRule (depends on * $outputTriggers) with initialization parameters: * - callback: {@see getJavascriptCallback()} * - element ID to set error for if validation fails * - error message to set if validation fails * - triggers: {@see getJavascriptTriggers()} (only for * qf.LiveRule when $outputTriggers is true) * - chained rules, array of arrays like in $chainedRules property * * @param bool $outputTriggers Whether the Rule will be run onblur / onchange * * @return string * @throws HTML_QuickForm2_Exception if Rule or its chained Rules can only * be run server-side */ public function getJavascript($outputTriggers = true) { HTML_QuickForm2_Loader::loadClass('HTML_QuickForm2_JavascriptBuilder'); $js = $this->getJavascriptCallback() . ",\n\t'" . $this->owner->getId() . "', " . HTML_QuickForm2_JavascriptBuilder::encode($this->getMessage()); $js = $outputTriggers && count($triggers = $this->getJavascriptTriggers()) ? 'new qf.LiveRule(' . $js . ', ' . HTML_QuickForm2_JavascriptBuilder::encode($triggers) : 'new qf.Rule(' . $js; if (count($this->chainedRules) > 1 || count($this->chainedRules[0]) > 0) { $chained = array(); foreach ($this->chainedRules as $item) { $multipliers = array(); /* @var $multiplier HTML_QuickForm2_Rule */ foreach ($item as $multiplier) { $multipliers[] = $multiplier->getJavascript(false); } $chained[] = '[' . implode(",\n", $multipliers) . ']'; } $js .= ",\n\t [" . implode(",\n", $chained) . "]"; } return $js . ')'; }
/** * Appends an element to the container * * If the element was previously added to the container or to another * container, it is first removed there. * * @param HTML_QuickForm2_Node Element to add * @return HTML_QuickForm2_Node Added element * @throws HTML_QuickForm2_InvalidArgumentException */ public function appendChild(HTML_QuickForm2_Node $element) { if (null !== ($container = $element->getContainer())) { $container->removeChild($element); } // Element can be renamed only after being removed from container $this->renameChild($element); $element->setContainer($this); $this->elements[] = $element; return $element; }
/** * (( defined by alex@cgi-central.net - it is too wordy to build * rules without such easy chaining )) * Adds a validation rule to the owner of this validation rule * * @param HTML_QuickForm2_Rule|string Validation rule or rule type * @param string Message to display if validation * @param mixed Additional data for the rule * @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, $message = '', $options = null) { return $this->owner->addRule($rule, $message, $options); }
/** * Finds a proper callback for the element * * Callbacks are scanned in a predefined order. First, if a callback was * set for a specific element by id, it is returned, no matter if the * element belongs to a group. If the element does not belong to a group, * we try to match a callback using the element class. * But, if the element belongs to a group, callbacks are first looked up * using the containing group id, then using the containing group class. * When no callback is found, the provided default callback is returned. * * @param HTML_QuickForm2_Node Element being rendered * @param mixed Default callback to use if not found * @return mixed Callback */ public function findCallback(HTML_QuickForm2_Node $element, $default = null) { if (!empty($this->callbacksForId[$element->getId()])) { return $this->callbacksForId[$element->getId()]; } $class = strtolower(get_class($element)); $groupId = end($this->groupId); $elementClasses = array(); do { if (empty($groupId) && !empty($this->callbacksForClass[$class])) { return $this->callbacksForClass[$class]; } $elementClasses[$class] = true; } while ($class = strtolower(get_parent_class($class))); if (!empty($groupId)) { if (!empty($this->elementCallbacksForGroupId[$groupId])) { while (list($elClass) = each($elementClasses)) { if (!empty($this->elementCallbacksForGroupId[$groupId][$elClass])) { return $this->elementCallbacksForGroupId[$groupId][$elClass]; } } } $group = $element->getContainer(); $grClass = strtolower(get_class($group)); do { if (!empty($this->elementCallbacksForGroupClass[$grClass])) { reset($elementClasses); while (list($elClass) = each($elementClasses)) { if (!empty($this->elementCallbacksForGroupClass[$grClass][$elClass])) { return $this->elementCallbacksForGroupClass[$grClass][$elClass]; } } } } while ($grClass = strtolower(get_parent_class($grClass))); } return $default; }
/** * Performs the server-side validation * * This method also calls validate() on all contained elements. * * @return boolean Whether the container and all contained elements are valid */ protected function validate() { $valid = true; foreach ($this as $child) { $valid = $child->validate() && $valid; } $valid = parent::validate() && $valid; // additional check is needed as a Rule on Container may set errors // on contained elements, see HTML_QuickForm2Test::testFormRule() if ($valid) { foreach ($this->getRecursiveIterator() as $item) { if (0 < strlen($item->getError())) { return false; } } } return $valid; }
/** * Performs the server-side validation * * This method also calls validate() on all contained elements. * * @return boolean Whether the container and all contained elements are valid */ protected function validate() { $valid = parent::validate(); foreach ($this as $child) { $valid = $child->validate() && $valid; } return $valid; }
/** * Creates an array with fields that are common to all elements * * @param HTML_QuickForm2_Node Element being rendered * * @return array */ public function buildCommonFields(HTML_QuickForm2_Node $element) { $keyn = $this->options['key_id'] ? 'id' : 'name'; $ary = array('id' => $element->getId(), 'frozen' => $element->toggleFrozen(), 'name' => $element->getName()); // Key that we use for putting into arrays so that smarty can extract them. // Note that the name may be empty. $key_val = $ary[$keyn]; if ($key_val == '') { $key_val = $ary['id']; } if ($labels = $element->getLabel()) { if (!is_array($labels) || !$this->options['static_labels']) { $ary['label'] = $labels; } else { foreach ($labels as $key => $label) { $key = is_int($key) ? $key + 1 : $key; if (1 === $key) { $ary['label'] = $label; } else { $ary['label_' . $key] = $label; } } } } // Smarty: group_errors under 'name' or 'id' depending on key_id option: if (($error = $element->getError()) && $this->options['group_errors']) { $this->array['errors'][$key_val] = $error; } elseif ($error) { $ary['error'] = $error; } if (isset($this->styles[$key_val])) { $ary['style'] = $this->styles[$key_val]; } if (!$element instanceof HTML_QuickForm2_Container) { $ary['html'] = $element->__toString(); } else { $ary['elements'] = array(); $ary['attributes'] = $element->getAttributes(true); } return $ary; }