如何显示 sfWidgetFormSelectRadio 项而不显示标签

发布于 2024-09-10 08:01:52 字数 1020 浏览 2 评论 0原文

我在 Ubuntu 10.0.4 上使用 Symfony 1.3.6。

我正在使用 sfWidgetFormSelectRadio 来允许用户从表单中的列表中选择图片。

在操作中,图片设置如下:

    $this->form->setWidget('chosenpic', new sfWidgetFormSelectRadio(array(
                            'choices' => $this->pictures,
                            'default' => $this->pictures[count($this->pictures)-1] ))
                            );

在模板中,小部件显示如下:

<?php echo $form['chosenpic']->render(array('id'=>'check'.($i+1), 'value'=> ($i+1), 'width' => "80", 'height' => "80")); ?>

这会生成以下输出:

<ul class="radio_list"><li><input type="radio" width="80" height="80" id="check1" value="1" name="flowers[chosenpic]">&nbsp;<label for="flowers_chosenpic_0">http://example.com/media/images/48408000/jpg/_48408794_009829449-1.jpg</label></li></ul>

我不希望出现标签,因为它会弄乱表单。如果没有自己手动生成 HTML(使用表单和小部件名称),有没有办法可以阻止小部件显示“标签”?

I am using Symfony 1.3.6 on Ubuntu 10.0.4.

I am using the sfWidgetFormSelectRadio to allow a user to select a picture from a list, in a form.

In the action, the pictures are set up like this:

    $this->form->setWidget('chosenpic', new sfWidgetFormSelectRadio(array(
                            'choices' => $this->pictures,
                            'default' => $this->pictures[count($this->pictures)-1] ))
                            );

In the template, the widget is displayed like this:

<?php echo $form['chosenpic']->render(array('id'=>'check'.($i+1), 'value'=> ($i+1), 'width' => "80", 'height' => "80")); ?>

This generates the following output:

<ul class="radio_list"><li><input type="radio" width="80" height="80" id="check1" value="1" name="flowers[chosenpic]"> <label for="flowers_chosenpic_0">http://example.com/media/images/48408000/jpg/_48408794_009829449-1.jpg</label></li></ul>

I do no want the the label for appearing, as it messes up the form. Short of manually generating the HTML myself (using the form and widget names, is there a way that I can prevent the widget from displaying a 'label for' ?

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

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

发布评论

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

评论(2

桃扇骨 2024-09-17 08:01:52

您需要提供自定义格式化程序。在表单的配置函数中,执行以下操作:

public function configure()
{
  $this->setWidget('chosenpic', new sfWidgetFormSelectRadio(array(
        'formatter' => array($this, 'pictureRadioFormatterCallback')
  )));
}

然后,添加一个不向表单呈现标签的格式化程序:

public function pictureRadioFormatterCallback($widget, $inputs)
{
  $rows = array();
  foreach ($inputs as $input)
  {
    $rows[] = $widget->renderContentTag('li', $input['input']);
  }

  return !$rows ? 
    '' : 
    $widget->renderContentTag('ul', implode($widget->getOption('separator'), $rows), array('class' => $widget->getOption('class')));
}

但是,理想情况下,您应该使用标签来显示图像,而不是在标签之外显示图像。另外,在操作内配置小部件通常是一个坏主意,该代码可以采用自己的形式吗?

You need to provide a custom formatter. In your form's configure function, do the following:

public function configure()
{
  $this->setWidget('chosenpic', new sfWidgetFormSelectRadio(array(
        'formatter' => array($this, 'pictureRadioFormatterCallback')
  )));
}

Then, add a formatter that doesn't render labels to your form:

public function pictureRadioFormatterCallback($widget, $inputs)
{
  $rows = array();
  foreach ($inputs as $input)
  {
    $rows[] = $widget->renderContentTag('li', $input['input']);
  }

  return !$rows ? 
    '' : 
    $widget->renderContentTag('ul', implode($widget->getOption('separator'), $rows), array('class' => $widget->getOption('class')));
}

However, ideally, you should be using the labels to display the images, rather than displaying the images outside of the labels. Also, it's typically a bad idea to be configuring widgets inside of an action, can that code go in it's own form?

戏舞 2024-09-17 08:01:52

您可以执行上述操作 - 或者使用 CSS 隐藏默认生成的标签...类似于:

ul.radio_list li input label { display: none; }

表单本身可能有一个 #id 来帮助您更好地指定 CSS 选择器。但如果您喜欢向您的应用程序添加样板 PHP 代码,请随意:)

You can do the above -- or use CSS to hide the label generated by default ... something along the lines of:

ul.radio_list li input label { display: none; }

the form itself probably has an #id to help you better specify the CSS selector better. But if you like adding boilerplate PHP code to your app feel free :)

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