Symfony 中的多种形式

发布于 2024-12-29 21:13:55 字数 688 浏览 1 评论 0原文

我确实有一个表格。例如,此表单提交 3 个单词(啤酒、可乐、葡萄酒)。在下一个操作中,我确实希望有一个具有一个或多个选择的三选择小部件:

-beer:                //first choice field
  * buddy lighty      //choice one
  * busch             //choice two
  * miler             //choice three

-coke:                //second choice field
  * coke diet
  * coke
  * coke vanilla

-wine:                //third choice field
  * bordeaux
  * suave
  * champange

<submit-button>

我希望在一个操作中实现每个选择。因此,如果有人做出选择,busch、coke、suave 将会被提交。我怎样才能意识到它?

更新:

感谢您的评论。我可能忘记说我不知道​​我需要多少个下拉菜单。可能只有啤酒和可乐,或者啤酒、可乐、葡萄酒和果汁。这取决于用户之前填写的表单数量的操作!我尝试在 forms.class.php 中使用 foreach 循环来完成此操作。但这没有帮助。

我用的是教义。

I do have a form. This form submits for example 3 words (beer, coke, wine). In the next action I do want to have a three choice widgets with one or more choices:

-beer:                //first choice field
  * buddy lighty      //choice one
  * busch             //choice two
  * miler             //choice three

-coke:                //second choice field
  * coke diet
  * coke
  * coke vanilla

-wine:                //third choice field
  * bordeaux
  * suave
  * champange

<submit-button>

I want every choice in one action. So if somebodey make a choice busch, coke, suave will be submittet. How can I realise it?

Update:

Thanks for the comment. I might forget to say that I don't know how many dropdown menus I need. There might be just beer and coke or beer, coke, wine and juice. It depends from what the user fill out the number of forms the action before! I tried to do it with a foreach-loop in forms.class.php. But it doesn't help.

I use Doctrine.

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

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

发布评论

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

评论(1

枫林﹌晚霞¤ 2025-01-05 21:13:55

执行此操作的一种简单方法(也取决于您的模型)是将每个项目配置为可为空,然后使用表单选项来显示/隐藏某些小部件。例如,如果您的模式看起来像这个懒惰的示例:

DrinkOrder:
  columns:

    # ...

    beer:
      type: enum
      values: [Old Peculier,Tribute,Deuchars]
      notnull: false

     wine:
         type: enum
         values: [Bordeaux,Suave,Champagne]
         notnull: false

    # ...etc

像这样配置您的表单:

class DrinkOrderForm extends BaseDrinkOrderForm
{
    public function configure()
    {
        if ($this->getOption('hide_wine'))
        {
            $this->widgetSchema['wine'] = new sfWidgetFormInputHidden;
        }

        // … etc
    }
}

然后当上一个表单的操作提交时,您可以将选项传递给表单,例如:

$this->form = new DrinkOrderForm($drink_order, array(
    'hide_wine' => true,
    'hide_beer' => false,
));

这只是一个简单的示例 - 而不是 ENUM 类型,您可以使用与另一个表的关系(例如 wine_id 和 sfWidgetFormDoctrineChoice 小部件和验证器)。

您不能做的一件事是拥有 3-4 个单独的表单,因为网络浏览器只会提交其中之一。您要么必须将表单相互嵌入,要么使用上面更简单的技术,具体取决于模型的设置方式。

如果选择的类型数量不固定,那么您需要考虑使用表单系统的 embedRelation 方法(或 ahDoctrineEasyEmbeddedRelationsPlugin)之类的方法来动态添加子表单。从你的例子中很难知道你想走多远。 :)

One simple way to do this (depends on your model, too) is to configure each item as nullable, and then use form options to show/hide certain widgets. e.g., if your schema looks like this lazy example:

DrinkOrder:
  columns:

    # ...

    beer:
      type: enum
      values: [Old Peculier,Tribute,Deuchars]
      notnull: false

     wine:
         type: enum
         values: [Bordeaux,Suave,Champagne]
         notnull: false

    # ...etc

Configure your form like this:

class DrinkOrderForm extends BaseDrinkOrderForm
{
    public function configure()
    {
        if ($this->getOption('hide_wine'))
        {
            $this->widgetSchema['wine'] = new sfWidgetFormInputHidden;
        }

        // … etc
    }
}

And then when the action of the previous form submits you can pass options to the form, like:

$this->form = new DrinkOrderForm($drink_order, array(
    'hide_wine' => true,
    'hide_beer' => false,
));

This is just a quick example - instead of an ENUM type, you could use relations to another table (e.g. wine_id and an sfWidgetFormDoctrineChoice widget & validator).

One thing you can't do is have 3-4 separate forms, because web browsers will only submit one of them. You either have to embed forms within each other, or use the simpler technique above, depending on how your model's set up.

If the number of types of choice isn't fixed, then you'd want to look into using something like the form system's embedRelation method (or the ahDoctrineEasyEmbeddedRelationsPlugin) to dynamically add sub-forms. It's hard to know from your example just how far you want to go. :)

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