Code snippets for symfony 1.x

Navigation

Converting the form fields in paragraphs with a decorator

I created a reusable object, where I want to display select fields blocked, because this way I display only the value in DIVs, adding an input hidden (or not).

Not resolved the idea of a print version because only a few fields have the option renderer_class.

If someone yells at me to think of something :)

Decorator

<?php
 
class g1mrWidgetFormPrint extends sfWidgetFormInputHidden
{
 
  protected function configure($options = array(), $attributes = array())
  {
    $this->addOption('choices');
    $this->addOption('template');
    $this->addOption('renderHidden', true);
    $this->addOption('class', 'form_print');
    $this->addOption('break', "<br>\n");
    $this->addOption('print_template', <<<EOF
<div class="%class%">
    %value%
    %hiddenField%
</div>
EOF
);
   parent::configure($options, $attributes);
  }
 
  /**
   * Renders the widget.
   *
   * @param  string $name        The element name
   * @param  string $value       The value selected in this widget
   * @param  array  $attributes  An array of HTML attributes to be merged with the default HTML attributes
   * @param  array  $errors      An array of errors for the field
   *
   * @return string An HTML tag string
   *
   * @see sfWidgetForm
   */
  public function render($name, $value = null, $attributes = array(), $errors = array())
  {
    if (is_null($value))
    {
      $value = '';
    }
 
    $choices = $this->getOption('choices');
    if ($choices instanceof sfCallable)
    {
      $choices = $choices->call();
    }
 
      if (is_array($choices)) 
      {  
        $vc = $value;
        if (!is_array($vc))
        {
         $vc= array($vc);
        }
        foreach ($choices as $k => $array)
        {
          if (is_array($array))
          {
            foreach($array as $key => $o)
            {
              if (in_array(strval($key), $vc))
              {
                 $associated[$key] = $o;
              }
            }  
          }
          else
          {
            foreach($choices as $key => $o)
            {
              if (in_array(strval($key), $vc))
              {
                 $associated[$key] = $o;
              }
            }
 
          }
        } 
      }
      else
      {
        $associated = $value;
      } 
 
    $hiddenField = '';
    if($this->getOption('renderHidden'))
    {
        $hiddenField = parent::render($name, $value, $attributes, $errors);
    }
 
    return strtr($this->getOption('print_template'), array(
      '%class%'              => $this->getOption('class'),
      '%value%'              => $this->processValue($associated),
      '%hiddenField%'        => $hiddenField
    ));
  }
 
  public function processValue($x)
  {
    if(is_array($x))
    {
      return implode($this->getOption('break'),$x); 
    }
    else
    {
      return $x;
    } 
  }
 
}
 

Form

//...
 
    public function setPrintable($field, $renderHidden = true)
    {
        if($this->widgetSchema[$field]->hasOption('renderer_class'))
        {
            $this->widgetSchema[$field]->setOption('renderer_class', 'g1mrWidgetFormPrint');
            $this->widgetSchema[$field]->setOption('renderer_options', array('renderHidden' => $renderHidden));
        }
    }
 
 
    public function setPrintables($renderHidden = true)
    {
        foreach($this->getFormFieldSchema()->getWidget()->getFields() as $key => $object)
        {
            $this->setPrintable($key, $renderHidden);
        }  
    }
 
//..    
 

Controler

//...
  public function executeEdit(sfWebRequest $request)
  {
    $this->pedido = $this->getRoute()->getObject();
    $this->form = $this->configuration->getForm($this->pedido);
    if($this->pedido->getIsSent() == 1)
    {
      $this->form->setPrintables(); 
    }
  }
//...
 
by Gilmar Pupo on 2010-10-02, tagged decorator  forms  widget