Symfony 1.4 displays images as choices in selection widgets

I have a form with a specific selection widget (DoctrineChoice). The selection relates to a specific image file on the server, and I use the expander = true parameter (for checkboxes / radio buttons)

Is there a way to display the widget by displaying images of each option? By default, I get only the parameter identifier in the database.

Using firebug, I noticed that the generated HTML has a tag with an identifier for each selection, and I also managed to change this with a specific image, so I assume that I need to do everything to change the text for the label of each selection. Despite the fact that the "label" option of the widget just changes the label for the entire select element, so this will not happen ...

Thank!

+3
source share
1 answer

Well, after a lot of research, I managed with some kind of solution, but maybe something is more correct?

Instead of using sfWidgetFormDoctrineChoice, I used sfWidgetFormSelectRadio (but Checkbox can also work, but I don’t know if it can work with other widgets or even select widgets: / just because my business rules require it, SelectRadio was enough in this particular case. ..)

The widget selection option was filled with the results of the previous query, which I used to populate the previous DoctrineChoice widget, which was previously processed, so the identifier of each record was the key and value of each choice:

$imgs = Doctrine_Core::getTable('ProjImages')->getImages();
$choices = array('' => '');
foreach ($imgs as $img):
  $choices[$img->getId()] = $img->getId();
endforeach;

Then I also passed the "formatter" widget:

$this->widgetSchema['img'] = new sfWidgetFormSelectRadio(array(
                    'choices' => $choices,
                    'formatter' => array($this, 'showAsImages')
                                ));
$this->validatorSchema['img'] = new sfValidatorChoice(array(
                     'choices' => $choices,
                     'required' => false
                     ));

"required" = > false , " " , $options ('' = > '').

, :

public function showAsImages($widget, $inputs)
{
  $rows = array();
  foreach ($inputs as $input)
  {
    $domdoc = new DOMDocument();
    $domdoc->loadHTML($input['label']);
    $node = $domdoc->getElementsByTagName('label')->item(0);
    if ($node->nodeValue != "")
    {
      $img = Doctrine_Core::getTable('ProjImages')->find(array($node->nodeValue));
      $input['label'] = '<label '.$node->attributes->item(0)->name .
                        '="'.$node->attributes->item(0)->value.'">' .
                        '<img src="'.$img->getImg().'" alt="image" />' .
                        '</label>';
    }
    $rows[] = $widget->renderContentTag('li',
                    $input['input'].
                    $widget->getOption('label_separator').
                    $input['label']);
  }
  return $widget->renderContentTag('ul',
                       implode($widget->getOption('separator'), $rows),
                       array('class' => $widget->getOption('class')));
}

sfWidgetFormSelectRadio "" ( , , ).

DOMDocument ( ), , "" < lt; img > . , , 'label'...

... , , ... , "", , , , ...

!

+2

Source: https://habr.com/ru/post/1794227/


All Articles