Symfony plain/choice widget (sfWidgetFormChoiceOrPlain)


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



Copy this code and paste it in your HTML
  1. <?php
  2.  
  3. /**
  4.  * Sample usage: $this->widgetSchema['type'] = new sfWidgetFormChoiceOrPlain(array('choices' => sfConfig::get('types'), 'plain' => !$this->getObject()->isNew()));
  5.  *
  6.  * @author Luã de Souza
  7.  */
  8. class sfWidgetFormChoiceOrPlain extends sfWidgetFormChoice
  9. {
  10. protected function configure($options = array(), $attributes = array())
  11. {
  12. parent::configure($options, $attributes);
  13.  
  14. $this->addOption('plain', false);
  15. }
  16.  
  17. public function render($name, $value = null, $attributes = array(), $errors = array())
  18. {
  19. if ($this->getOption('plain')) {
  20.  
  21. $attributes = array_merge(array('class'=>'frozen'), $attributes);
  22.  
  23. $hidden = new sfWidgetFormInputHidden();
  24. $choices = $this->getOption('choices');
  25. $label = $value ? $choices[$value] : $value;
  26.  
  27. return $this->renderContentTag('div', $label, $attributes) . $hidden->render($name, $value);
  28. } else {
  29. if ($this->getOption('multiple'))
  30. {
  31. $attributes['multiple'] = 'multiple';
  32.  
  33. if ('[]' != substr($name, -2))
  34. {
  35. $name .= '[]';
  36. }
  37. }
  38.  
  39. if (!$this->getOption('renderer') && !$this->getOption('renderer_class') && $this->getOption('expanded'))
  40. {
  41. unset($attributes['multiple']);
  42. }
  43.  
  44. return parent::getRenderer()->render($name, $value, $attributes, $errors);
  45. }
  46. }
  47. }

URL: http://www.lsouza.pro.br

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.