如何单独显示 sfWidgetFormChoice 的单选按钮?

发布于 2024-10-19 19:39:01 字数 212 浏览 7 评论 0原文

我想以这种方式呈现 symfony 表单:

form with radio Button

我的问题涉及单选按钮小部件:我不不知道如何单独呈现每个选择,以便在这些单选按钮之间显示其他字段。

有没有办法用 symfony 形式来实现这一点?

谢谢!

I'd like to render a symfony form this way:

form with radio buttons

My problem concerns the radio buttons widget: I don't know how to render each choice individually, in order to display other fields between those radio buttons.

Is there a way to achieve this with symfony forms?

Thanks!

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

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

发布评论

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

评论(3

入怼 2024-10-26 19:39:01

Symfony 让这件事变得有点困难。您想要一个可从视图调用的 renderChoice( $value) 方法,但这需要更改或扩展 sfFormField。

更改它不是面向未来的,扩展它还需要您更改一些 symfony 变量,以便您视图中可访问的字段是这个新类的实例。我不知道这个选项是否存在。

相反,我选择将 sfWidgetFormSelectRadio 扩展为以下非常简单的类,该类扩展了 formatChoices。它将检查您是否只要求一种选项。如果是,它会渲染该标签并返回它,否则它会通过 parent:: 正常渲染。

这可以让您从视图中调用:$form['field']->render(array('only_choice'=> $value))
其中 $value 是要呈现的单选选项的索引:

<?php

/**
 * sfWidgetFormSelectRadioSingleable lets you render just one option at a time.
 *
 * @package    symfony
 * @subpackage widget
 * @author     Fabien Potencier <[email protected]>
 * @version    SVN: $Id: sfWidgetFormSelectRadio.class.php 27738 2010-02-08 15:07:33Z Kris.Wallsmith $
 */
class sfWidgetFormSelectRadioSingleable extends sfWidgetFormSelectRadio
{
  public function formatChoices($name, $value, $choices, $attributes)
  {
    $onlyChoice = (!empty($attributes['only_choice'])) ? $attributes['only_choice'] : false; unset($attributes['only_choice']);

    if($onlyChoice)
    {
      if(!isset($choices[$onlyChoice]))
          throw new Exception("Option '$onlyChoice' doesn't exist.");

      $key    = $onlyChoice;
      $option = $choices[$key];
      $baseAttributes = array(
        'name'  => substr($name, 0, -2),
        'type'  => 'radio',
        'value' => self::escapeOnce($key),
        'id'    => $id = $this->generateId($name, self::escapeOnce($key)),
      );

      if (strval($key) == strval($value === false ? 0 : $value))
      {
        $baseAttributes['checked'] = 'checked';
      }

      return $this->renderTag('input', array_merge($baseAttributes, $attributes));
    }
    else
    {
      return parent::formatChoices($name, $value, $choices, $attributes);
    }
  }
}

代码很尴尬,但在实践中很容易使用,并且将来不会中断。

Symfony makes this a little difficult to do. You'd like a renderChoice( $value) method that's callable from the view, but that would require changing or extending sfFormField.

Changing it isn't future proof, and extending it would also require you to change some symfony variable so the fields accessible in your view are instances of this new class. I don't know if that option exists.

Instead, I opted to extend sfWidgetFormSelectRadio into the following very simple class which extends formatChoices. It will check if you're asking for only one option. If you are, it renders that tag and returns it, otherwise it renders normally via parent::.

This lets you call from your view: $form['field']->render(array('only_choice'=> $value))
where $value is the index of the radio option you want to render:

<?php

/**
 * sfWidgetFormSelectRadioSingleable lets you render just one option at a time.
 *
 * @package    symfony
 * @subpackage widget
 * @author     Fabien Potencier <[email protected]>
 * @version    SVN: $Id: sfWidgetFormSelectRadio.class.php 27738 2010-02-08 15:07:33Z Kris.Wallsmith $
 */
class sfWidgetFormSelectRadioSingleable extends sfWidgetFormSelectRadio
{
  public function formatChoices($name, $value, $choices, $attributes)
  {
    $onlyChoice = (!empty($attributes['only_choice'])) ? $attributes['only_choice'] : false; unset($attributes['only_choice']);

    if($onlyChoice)
    {
      if(!isset($choices[$onlyChoice]))
          throw new Exception("Option '$onlyChoice' doesn't exist.");

      $key    = $onlyChoice;
      $option = $choices[$key];
      $baseAttributes = array(
        'name'  => substr($name, 0, -2),
        'type'  => 'radio',
        'value' => self::escapeOnce($key),
        'id'    => $id = $this->generateId($name, self::escapeOnce($key)),
      );

      if (strval($key) == strval($value === false ? 0 : $value))
      {
        $baseAttributes['checked'] = 'checked';
      }

      return $this->renderTag('input', array_merge($baseAttributes, $attributes));
    }
    else
    {
      return parent::formatChoices($name, $value, $choices, $attributes);
    }
  }
}

Awkward code, but easy enough to use in practice and it won't break in the future.

满栀 2024-10-26 19:39:01

标准小部件不允许您在一个字段的元素之间插入另一个字段;请参阅 sfWidgetFormSelectRadio::formatChoices() 来了解原因。我的第一反应是扩展 sfWidgetFormSelectRadio,包括一个 renderChoice( $value ) 方法。

The standard widgets are not going to allow you to inject another field in between elements of one field; see sfWidgetFormSelectRadio::formatChoices() for why this is. My first reaction is to extend sfWidgetFormSelectRadio, including a renderChoice( $value ) method.

坏尐絯℡ 2024-10-26 19:39:01

如何定义具有相同名称的单独 sfWidgetFormSelectRadio 小部件和不同的价值观?

How about defining separate sfWidgetFormSelectRadio widgets with the same name and different values?

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