Beispiel #1
0
 /**
  * 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;
 }
Beispiel #2
0
 /**
  * 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}";
 }
Beispiel #3
0
 /**
  * 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;
 }
Beispiel #4
0
 /**
  * Renders a generic element
  *
  * @param HTML_QuickForm2_Node $element Element being rendered
  */
 public function renderElement(HTML_QuickForm2_Node $element)
 {
     if ($element->isRequired()) {
         $this->required = true;
     }
     if ($this->options['group_errors'] && ($error = $element->getError())) {
         $this->errors[$element->getId()] = $error;
     }
 }
Beispiel #5
0
 /**
  * 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)
 {
     $ary = array('id' => $element->getId(), 'frozen' => $element->toggleFrozen());
     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;
                 }
             }
         }
     }
     if (($error = $element->getError()) && $this->options['group_errors']) {
         $this->array['errors'][$ary['id']] = $error;
     } elseif ($error) {
         $ary['error'] = $error;
     }
     if (isset($this->styles[$ary['id']])) {
         $ary['style'] = $this->styles[$ary['id']];
     }
     if (!$element instanceof HTML_QuickForm2_Container) {
         $ary['html'] = $element->__toString();
     } else {
         $ary['elements'] = array();
         $ary['attributes'] = $element->getAttributes(true);
     }
     return $ary;
 }
Beispiel #6
0
 /**
  * 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($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();
             foreach ($item as $multiplier) {
                 $multipliers[] = $multiplier->getJavascript(false);
             }
             $chained[] = '[' . implode(",\n", $multipliers) . ']';
         }
         $js .= ",\n\t [" . implode(",\n", $chained) . "]";
     }
     return $js . ')';
 }
Beispiel #7
0
 /**
  * Finds a proper template for the element
  *
  * @param    HTML_QuickForm2_Node    Element being rendered
  * @param    string                  Default template to use if not found
  * @return   string  Template
  */
 public function findTemplate(HTML_QuickForm2_Node $element, $default = '{element}')
 {
     if (!empty($this->templatesForId[$element->getId()])) {
         return $this->templatesForId[$element->getId()];
     }
     $class = strtolower(get_class($element));
     $groupId = end($this->groupId);
     $groupTemplate = null;
     do {
         if (empty($groupId) && !empty($this->templatesForClass[$class])) {
             return $this->templatesForClass[$class];
         } elseif (!empty($groupId)) {
             if (!empty($this->groupedTemplates[$groupId][$class])) {
                 return $this->groupedTemplates[$groupId][$class];
             } elseif (empty($groupTemplate) && !empty($this->groupedTemplates[''][$class])) {
                 $groupTemplate = $this->groupedTemplates[''][$class];
             }
         }
     } while ($class = strtolower(get_parent_class($class)));
     return $groupId && !empty($groupTemplate) ? $groupTemplate : $default;
 }
Beispiel #8
0
 /**
  * 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;
 }