【问题标题】:Symfony - Add and persist extra fieldsSymfony - 添加并保留额外的字段
【发布时间】:2016-09-14 17:29:29
【问题描述】:

我有一个显示一些选项的 ChoiceType::Type 字段,我想为每个选项添加一个输入以在其上添加价格。我是这样做的:

->add('product_price', ChoiceType::class, array(
    'choices' => array(
       "Product 1",
       "Product 2",
    ),
)

为每个选项添加输入的 JS:

var productBoxes = $("[id^=product_]");
// Listen the checkbox to display or hide the prices inputs
productBoxes.each(function (index) {
    var priceField = '<label class="control-label required" for="product_price_' + index + '">Capacité</label>' +
        '<input type="text" id="product_price_' + index + '"  name="product[price][]" class="form-control">';
    $(this).click(function () {
        if ($(this).is(':checked')) {
            $(this).parent().append(priceField);
        } 
    })
})

javascript 可以工作,它会在每个选项旁边附加字段。现在我想将我的数据发送到一个数组中,如下所示: ["产品 1" => "附加字段的值"]

但我不知道如何获取这些额外数据并将其保存到数据库中。

有人知道怎么做吗?

编辑 1 我试图用 CollectionType 来做,但不知道如何将每个 CollectionType 元素呈现为复选框。有没有办法做到这一点?

感谢您的帮助!

【问题讨论】:

    标签: php symfony symfony-forms


    【解决方案1】:

    我认为更好的方法是创建自定义类型并在那里设置其他字段。

    例如:

    你的主要表单类型:

    $builder->add('product_price', CollectionType::class, array(
        'label' => 'Prices',
        'entry_type'    => ProductPriceType::class
    ));
    

    产品价格类型:

    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('product', TextType::class, array())
            ->add('price', NumericType::class, array());
    }
    
    
    /**
     * @param OptionsResolver @resolver
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'AppBundle\Entity\SomeEntity'
        ));
    }
    

    我认为您从基地获取产品数据。 在这种情况下,您将获得包含 2 个值的数组 - 产品和价格

    【讨论】:

    • 是的,我试过这样,但不知道如何 1. 将 CollectionType 呈现为复选框。 2.在产品选择上附加价格输入。有办法吗?
    • 您可以在 ProductPriceType 中添加复选框字段。它看起来像每个产品条目的复选框。然后保存已选中复选框的数组项
    【解决方案2】:

    在您的示例中,最简单的方法是在表单中再添加两个字段。 Keep them hidden with CSS (display: none), and just show them with JS (toggle class "hidden" when the choice gets un/selected)

    ->add('product_one_price', NumberType::class, array(
        'attr' => array('class' => 'hidden')
    ))
    ->add('product_two_price', NumberType::class, array(
        'attr' => array('class' => 'hidden')
    ))
    

    另一种方法是嵌套表单,或者动态构建表单,这可能会也可能不会过度,这取决于您实际在做什么

    【讨论】:

    • 感谢您的回答!但是通过这种方式,我的表格中每个产品需要 1 个字段,每个价格需要 1 个字段。我不要那个。嵌套表单是一种解决方案,但我真的每个产品 1 个复选框,每个复选框 1 个字段..! CollectionType 可以这样做吗?
    【解决方案3】:

    也许我错了,但我认为你应该深入研究 ChoiceType 类。

    当使用基本的 ChoiceType 时,Symfony 文档说:

    choices 选项是一个数组,其中数组键是项目的标签,数组值是项目的值

    如果我正确理解你的情况,你想要一些非常具体的东西,比如这种选择:

    $choices = [
         'item_label' => [
               'value' => 'item_value',
               'price' => 'item_price'
         ],
         'item_label2' => [
               'value' => 'item_value2',
               'price' => 'item_price2'
         ],
         ...
    ]
    

    我无法准确告诉您要覆盖哪个类,但您最好看一下:

    • ChoiceListFactory 类
    • ChoiceList 类(ChoiceType 使用子类 SimpleChoiceList)
    • ChoiceToValuesTransformer

    我有两个问题:

    • 存储标签和价格的数据模型是什么?

    • 这个列表是干什么用的?如果你在表单组件中挖掘太复杂,也许你应该把你的过程分成两个步骤:

      1. 一种用于定义产品价格的表格
      2. 一个表格来选择您感兴趣的产品

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-07
      • 2015-08-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-19
      • 1970-01-01
      相关资源
      最近更新 更多