首页 > 解决方案 > 将数据传递给集合类型 symfony 中的子表单

问题描述

我使用 symfony4.1 nad 我用构建器创建了表单,例如:

->add('products', CollectionType::class, array(
        'entry_type'    =>   FrontProductsType::class,
        'entry_options' => [
            'label' => 'Produkt',

        ],
        'label'        => 'Dodaj produkty',
        'allow_add'    => true,
        'allow_delete' => true,
        'prototype'    => true,
        'required'     => false,
        'attr'         => [
            'class' => 'products-select',
        ],
    ))

我的问题是如何将数据传递给集合类型内的这个实体类?

这是这个嵌入的形式:

 public function buildForm(FormBuilderInterface $builder, array $options)
  {
    $builder
    ->add('product', EntityType::class, array(
      'placeholder' => 'Wyszukiwarka',
      'mapped' => false,
      'multiple' => false,
      'class' => Products::class,
      'data' => $options['product'],

      'attr' => [
        'class' => 'chosen-select order_product',
        'data-placeholder' => 'Wybierz produkt',
        ],
        'choice_attr' => function($product) {
          $cena = $product->getJson()["products"]["cena"];
          $stock = $product->getJson()["products"]["stock"];
          $vat = $product->getJson()["products"]["vat"];
          return array(
            'class' => 'single_option',
            'single_price' => $cena,
            'stock_available' => $stock,
            'single_vat' => $vat,
          );
          },

          'choice_label' => function ($product) {
            return '' . $product->getJson()["products"]["name"] . ' | Stan Magazynowy: ' . $product->getJson()["products"]["stock"] . ' | Cena netto: ' . $product->getJson()["products"]["cena"] .'';
            },
            'label' => 'Wybierz produkty'

          ))
          ->add('quantity', IntegerType::class, [
            'label_attr' => array('class' => 'bmd-label-floating'),
            'label' => 'Ilość',
            'mapped' => false,
            'required' => true,
            'data' => $options['quantity'],
            'attr' => [
              'class' => 'form-control single-quantity-field',
              ]])
              ->add('cena', TextType::class, [
                'label_attr' => array('class' => 'bmd-label-floating'),

                'label' => 'Koszt brutto',
                'mapped' => false,
                'required' => true,
                'data' => $options['cena'],
                'attr' => [
                  'read_only' => true,
                  'class' => 'form-control single-price-field'],
                ]);

                $builder->get('product')->addEventListener(
                  FormEvents::POST_SUBMIT,
                  function (FormEvent $event) {
                    $form = $event->getForm();
                    $json = $form->getData()->getJson();
                    $data = $form->getData();
                    foreach ($json as $prop) {
                      foreach ($prop as $key => $value) {
                        $$key = $value;
                      }
                    }
                    $cena = number_format($cena, 2);
                    $form->getParent()->add('cena',TextType::class, [
                      'label_attr' => array('class' => 'bmd-label-floating'),
                      'label' => 'Koszt brutto',

                      'mapped' => false,
                      'required' => true,
                      'data' => $cena,
                      'attr' => [
                        'read_only' => true,
                        'class' => 'form-control single-price-field', 'value' => $cena],
                      ])
                      ->add('quantity', IntegerType::class, [
                        'label_attr' => array('class' => 'bmd-label-floating'),
                        'label' => 'Ilość',
                        'mapped' => false,
                        'required' => true,
                        'attr' => [
                          'class' => 'form-control single-quantity-field',
                          'min' => 1,
                          'max' => $stock,
                          ]]);
                        }
                      );
                    }


                    public function getBlockPrefix()
                    {
                      return 'product';
                    }

                    public function configureOptions(OptionsResolver $resolver)
                    {
                      $resolver->setDefaults([
                        'data_class' => Products::class,
                        'product' => null,
                        'quantity' => null,
                        'cena' => null
                      ]);
                    }

                  }

当我保存时,一切看起来都很好。当我传递一些产品时,数组中出现了许多产品,但没有传递值以嵌入形式设置。知道如何从 collectiontype 传递数据来填充子表单吗?

标签: phpformssymfony

解决方案


如果主表单(我们称之为YourFormType)没有data_class,您可以通过数组设置数据,使用字段名称进行索引。

$form = $this->createForm(YourFormType::class, ["products" => $array_of_Products]);

推荐阅读