/** * Enter description here ... * @param Zend_Form_Element $element * @author Tung Ly */ public function renderFormElement($element, $vertical = 0) { if ($element->getType() != "Zend_Form_Element_Checkbox") { $element->setAttrib('class', $element->getAttrib('class') . ' form-control'); } if ($element->isRequired()) { $element->setLabel($element->getLabel() . ' *'); $element->setAttrib('class', $element->getAttrib('class') . ' required'); } switch ($element->getType()) { case 'Zend_Form_Element_Textarea': $element->setAttrib('rows', 5); $element->setAttrib('cols', 80); break; case 'Zend_Form_Element_Hidden': return $element; default: break; } $error = ''; if ($element->hasErrors()) { $error = 'has-error'; } if ($element->getType() == 'Zend_Form_Element_Textarea') { } $btn = array('Zend_Form_Element_Submit', 'Zend_Form_Element_Reset'); if (in_array($element->getType(), $btn)) { //$t ='<button type="reset" class="btn"><i class="icon-refresh"></i> '.$element->getLabel().'</button>'; $t = '<div class="span2">' . $element . '</div>'; } else { $label = trim(preg_replace("/([A-Z])/", " \$1", "{$element->getLabel()}"), ' '); $variables = array('%%ERROR_CLASS%%' => $error, '%%ELEMENT_NAME%%' => $element->getName(), '%%ELEMENT_LABEL%%' => $label, '%%ELEMENT%%' => $element, '%%HELP_MESSAGE%%' => current($element->getMessages())); $t = str_replace(array_keys($variables), $variables, $this->_getTemplate($vertical)); } return $t; }
/** * Get value * * If element type is one of the button types, returns the label. * * @param Zend_Form_Element $element * @return string|null */ public function getValue($element) { if (!$element instanceof Zend_Form_Element) { return null; } $type = $element->getType(); if (in_array($type, $this->_buttonTypes)) { return $element->getLabel(); } return $element->getValue(); }
protected function _setDecorator(Zend_Form_Element $element) { $search = array('submit', 'reset', 'checkbox', 'radio', 'textarea', 'password'); $replace = array('button', 'button', 'checkbox', 'checkbox', 'text', 'text'); $type = strtolower(str_replace('Zend_Form_Element_', '', $element->getType())); $type = str_replace($search, $replace, $type); if (key_exists($type, $this->_standardDecorators)) { $element->setDecorators($this->_standardDecorators[$type]); if (method_exists($element, 'hasErrors') && $element->hasErrors()) { $decorator = $element->getDecorator('HtmlTag'); if (is_object($decorator)) { $decorator->setOption('class', $decorator->getOption('class') . ' ' . $this->_errorClass); } } } else { $element->loadDefaultDecorators(); } }
/** * Setup per-element properties like labels, and classes */ protected function setupSingleElement(Zend_Form_Element $elm) { // determine if this element has an error. (Will be used below) $elmHasError = count($elm->getMessages()) > 0; // set element values from the language pack $elm->setLabel($this->lang->get('form.label.' . $elm->getName())); // display info about required length if validator exists if ($elm->getValidator('StringLength')) { $elm->setDescription(sprintf($this->lang->get('form.description.' . $elm->getName()), $elm->getValidator('StringLength')->getMin(), $elm->getValidator('StringLength')->getMax())); } else { $elm->setDescription($this->lang->get('form.description.' . $elm->getName())); } // Duplicating type attr to classname in case we need to support IE6 // and want to be able to directly target the element without using // input[type=text] $zendType = $elm->getType(); $className = strtolower(substr($zendType, strrpos($zendType, '_') + 1)); $elm->setAttrib('class', $className); // wrap this stuff up in a html div with class 'element' $elm->addDecorator('HtmlTag', array('tag' => 'div', 'class' => 'element')); // determine if element has error and use that to determine prefix char. // 1. There seems to be no way to add html to the reqPrefix // 2. There seems to be no way to add a custom classname to the div tag if ($elm->getName() != 'submit') { $reqChar = $elmHasError ? '! ' : '* '; $elm->addDecorator('Label', array('placement' => 'prepend', 'tag' => 'div', 'requiredPrefix' => $reqChar)); } // use custom error decorator that attempts to replace default error // messages by the ones supplied by My_LanguagaPack $errorDecorator = new My_Decorator_Errors(); $errorDecorator->setLanguagePack($this->lang); $elm->addDecorator($errorDecorator); // wrap everything so far in a li tag, give it class error if elm has error // ATT: using array to create alias for allready used HtmlTag decorator $liClass = $elmHasError ? 'error' : ''; $elm->addDecorator(array('outerLi' => 'HtmlTag'), array('tag' => 'li', 'class' => $liClass)); }
/** * Add a new element * * $element may be either a string element type, or an object of type * Zend_Form_Element. If a string element type is provided, $name must be * provided, and $options may be optionally provided for configuring the * element. * * If a Zend_Form_Element is provided, $name may be optionally provided, * and any provided $options will be ignored. * * @param string|Zend_Form_Element $element * @param string $name * @param array|Zend_Config $options * @throws Zend_Form_Exception on invalid element * @return Zend_Form */ public function addElement($element, $name = null, $options = null) { if ($element instanceof Zend_Form_Element) { // type string $exploderesult = explode('_', $element->getType()); $type = lcfirst(trim(end($exploderesult))); if (null !== $options && $options instanceof Zend_Config) { $options = $options->toArray(); } // Load default decorators if (null === $options || !is_array($options)) { $options = array(); //Class is maybe not properly transfered to this element. We'll add class if it exists here. if ($element->class) { $options['class'] = $element->class; } } if (!array_key_exists('decorators', $options)) { $decorators = $this->getDefaultDecoratorsByElementType($type); if (!empty($decorators)) { $options['decorators'] = $decorators; } } // Elements type use 'form-control' class $element_fc = array('text', 'password', 'dateTime', 'dateTimeLocal', 'date', 'month', 'time', 'week', 'number', 'email', 'url', 'search', 'tel', 'color', 'textarea', 'select', 'multiselect'); if (in_array($type, $element_fc)) { if (null === $options) { $options = array('class' => 'form-control'); } elseif (array_key_exists('class', $options)) { if (!strstr($options['class'], 'form-control')) { $options['class'] .= ' form-control'; $options['class'] = trim($options['class']); } } else { $options['class'] = 'form-control'; } } $element->setOptions($options); } parent::addElement($element, $name, $options); }