symfony 1.4 将图像显示为选择小部件中的选项

发布于 2024-10-18 21:58:25 字数 283 浏览 8 评论 0原文

我有一个带有特定选择小部件(DoctrineChoice)的表单。这些选项引用服务器中的某些图像文件,我使用 Expander=true 选项(对于复选框/单选按钮)

有没有办法通过显示每个选项的图像来显示小部件?默认情况下,我只获取数据库中选项的 ID。

使用 firebug,我注意到生成的 HTML 有一个带有每个选择的 id 的标签,而且,我设法用某个图像更改它,所以我猜我需要做的就是更改每个选择的标签。尽管如此,小部件的“标签”选项只会更改整个选择的标签,所以这是行不通的......

谢谢!

I have a form with a certain choice widget (DoctrineChoice). The choices refer to certain image file in the server, and I use the expander=true option (for checkboxes/radiobuttons)

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

Using firebug, I noticed that the generated HTML has a tag with the id of each choice, and also, I managed to change this with a certain image, so I'm guessing that I all need to do is to change the text for the label of each choice. Even though, the 'label' option of the widget just changes the label for the whole select, so that won't do...

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我乃一代侩神 2024-10-25 21:58:25

好吧,经过大量研究,我已经找到了某种解决方案,但也许还有更正确的方法?

我没有使用 sfWidgetFormDoctrineChoice,而是使用了 sfWidgetFormSelectRadio(但 Checkbox 也可以,但我不知道它是否可以与其他小部件一起使用,甚至也可以选择小部件:/只是因为我的业务规则需要它,SelectRadio 就足够了在这种特殊情况下...)

小部件的选择选项填充了我用来填充先前 DoctrineChoice 小部件的上一个查询的结果,之前进行了处理,因此每个记录的 Id 是每个选择的键和值:

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

接下来,我还将“格式化程序”选项传递给小部件:

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

我在验证器中使用了“required”=> false 选项,因为我还需要在小部件中选择“无图像”的选项,这反映在 $选择数组作为第一个 ('' => '') 选择。

最后,我编写了格式化程序回调:

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 对象来获取值(图像的 id),然后进行数据库查询来获取图像,然后用 << 重新组装“标签”。图像>标签...当然,如果我碰巧找到空的选择,我会使用默认的“标签”...

就是这样...我认为格式化程序回调可以做更多的工作,所以任何建议,甚至欢迎更好的问题解决方案...如您所见,我依赖于小部件的“格式化程序”选项,据我所知,只有某些小部件接受此选项...

感谢您的阅读!

Ok, after a lot of research, I have managed some kind of solution, but maybe there's something more correct?

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

The choices option of the widget was filled with the results of the previous query I was using to fill the previous DoctrineChoice widget, previously processed so the Id 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;

Next, I also passed the 'formatter' option to the widget:

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

I used the 'required'=>false option in the validator since I also need the option to select 'no image' in my widget, which is reflected in the $choices array as the first ('' => '') choice.

Finally, I wrote the formatter callback:

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')));
}

I used the source code for the original default formatter of the sfWidgetFormSelectRadio and based on it, I modified the 'label' of each input element (all the rest of the code is exactly the same as the source code I used).

And for the label of each input element, I used the DOMDocument object to get the value (id of the image), then make a DB query to get the image, and then re-assemble the 'label' with the < img > tag... Of course, if I happen to find the empty choice, I use the default 'label'...

And that was it... I think the formatter callback can get some more work, so any suggestions, or even better solutions to the problem, are welcomed... As you can see, I depend on the 'formatter' option of the widget, and as far a I can see, only some widgets accept this option...

Thanks for reading!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文