Custom Zend_Form: Changed Decorators & added methods


/ Published in: PHP
Save to your folder(s)

2 main objectives:

- change default decorators of element and form
- `setValues()` mainly used for persisting form entry from request params across requests


Copy this code and paste it in your HTML
  1. class Application_Form_Abstract extends Zend_Form {
  2. function loadDefaultDecorators() {
  3. if ($this->loadDefaultDecoratorsIsDisabled()) {
  4. return $this;
  5. }
  6.  
  7. // for elements
  8. $decorators = $this->_elementDecorators;
  9. if (empty($decorators)) {
  10. $this->setElementDecorators(array(
  11. 'ViewHelper',
  12. 'Errors',
  13. array('Description', array('tag' => 'p')),
  14. 'Label',
  15. array('HtmlTag', array('tag' => 'p'))
  16. ));
  17. $this->getElement('submit')->removeDecorator('Label');
  18. }
  19.  
  20. // for form
  21. $decorators = $this->getDecorators();
  22. if (empty($decorators)) {
  23. $this->addDecorator('FormElements')
  24. ->addDecorator('Description', array('placement' => 'PREPEND', 'tag' => 'p'))
  25. ->addDecorator('Form');
  26. }
  27. return $this;
  28. }
  29. function setValue($element, $value) {
  30. $this->getElement($element)->setValue($value);
  31. }
  32. function setValues($values) {
  33. foreach ($this->getElements() as $elem) {
  34. if (!$elem->getIgnore() && array_key_exists($elem->getName(), $values)) {
  35. $elem->setValue($values[$elem->getName()]);
  36. }
  37. }
  38. }
  39. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.