Return to Snippet

Revision: 46133
at May 13, 2011 08:14 by hisamu


Initial Code
<?php

/**
 * Sample usage: $this->widgetSchema['type'] = new sfWidgetFormChoiceOrPlain(array('choices' => sfConfig::get('types'), 'plain' => !$this->getObject()->isNew()));
 *
 * @author     Luã de Souza
 */
class sfWidgetFormChoiceOrPlain extends sfWidgetFormChoice
{
  protected function configure($options = array(), $attributes = array())
  {
    parent::configure($options, $attributes);

    $this->addOption('plain', false);
  }

  public function render($name, $value = null, $attributes = array(), $errors = array())
  {
    if ($this->getOption('plain')) {
      
      $attributes = array_merge(array('class'=>'frozen'), $attributes);
      
      $hidden   = new sfWidgetFormInputHidden();
      $choices  = $this->getOption('choices');
      $label    = $value ? $choices[$value] : $value;

      return $this->renderContentTag('div', $label, $attributes) . $hidden->render($name, $value);
    } else {
      if ($this->getOption('multiple'))
      {
        $attributes['multiple'] = 'multiple';

        if ('[]' != substr($name, -2))
        {
          $name .= '[]';
        }
      }

      if (!$this->getOption('renderer') && !$this->getOption('renderer_class') && $this->getOption('expanded'))
      {
        unset($attributes['multiple']);
      }

      return parent::getRenderer()->render($name, $value, $attributes, $errors);
    }
  }
}

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

Initial Description

                                

Initial Title
Symfony plain/choice widget (sfWidgetFormChoiceOrPlain)

Initial Tags
php

Initial Language
PHP