Symfony 4:JSON 形成集合

Symfony 4: JSON to Form Collection

我正在处理将通过 JSON (FOSRestBundle) 填充的 FormType。此表单有一个 CollectionType,我将 entry_type 设置为另一个 FormType。

但无论我尝试什么,我最终都会遇到错误:此表单不应包含额外字段。

产品系列:

class ProductCollection
{
    const GROUP_PRODUCT_COLLECTION_LIST = "product_collection_list";

    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=128)
     */
    private $name;

    /**
     * @ORM\OneToMany(targetEntity="GamePoint\Shop\Domain\ShopPackage\ShopPackage", mappedBy="productCollection")
     */
    private $shopPackages;

    /**
     * @ORM\OneToMany(targetEntity="GamePoint\Shop\Domain\ProductProductCollection\ProductProductCollection", mappedBy="productCollection")
     */
    private $productProductCollections;

产品系列类型:

class ProductCollectionType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name', TextType::class)
            ->add('productProductCollections', CollectionType::class, [
                'entry_type' => ProductProductCollectionType::class
            ])
        ;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => ProductCollection::class,
            'csrf_protection' => false,
        ]);
    }
}

ProductProductCollection:

class ProductProductCollection
{
    /**
     * @ORM\Column(type="json")
     */
    private $settings = [];

    /**
     * @ORM\Id()
     * @ORM\ManyToOne(targetEntity="GamePoint\Shop\Domain\Product\Product", inversedBy="productProductCollections")
     */
    private $product;

    /**
     * @ORM\Id()
     * @ORM\ManyToOne(targetEntity="GamePoint\Shop\Domain\ProductCollection\ProductCollection", inversedBy="productProductCollections")
     */
    private $productCollection;

ProductProductCollectionType:

class ProductProductCollectionType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('settings')
            ->add('product', EntityType::class, ['required' => false, 'class' => Product::class])
            ->add('productCollection', EntityType::class, ['required' => false, 'class' => ProductCollection::class])
        ;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => ProductProductCollection::class,
            'csrf_protection' => false,
        ]);
    }
}

JSON 发送至路线:

{
    "product_collection": {
        "name": "cheap stuff",
        "productProductCollections": [
            {
                "product": 1,
                "settings" : "['test' => '123']"
            },
            {
                "product": 2,
                "settings" : "['test' => '123']"
            }
        ]
    }
}

参数包:

+request: ParameterBag {#537
    #parameters: array:1 [
      "product_collection" => array:2 [
        "name" => "cheap stuff"
        "productProductCollections" => array:2 [
          0 => array:2 [
            "product" => 1
            "settings" => "['test' => '123']"
          ]
          1 => array:2 [
            "product" => 2
            "settings" => "['test' => '123']"
          ]
        ]
      ]
    ]
  }

每当我将此 JSON 发送到 ProductCollectionType 时,我都会收到以下 FormErrorIterator:

array (size=1)
  0 => 
    object(Symfony\Component\Form\FormError)[613]
      protected 'messageTemplate' => string 'This form should not contain extra fields.' (length=42)
      protected 'messageParameters' => 
        array (size=1)
          '{{ extra_fields }}' => string '"0", "1"' (length=8)
      protected 'messagePluralization' => null
      private 'message' => string 'This form should not contain extra fields.' (length=42)

它说带有 ProductProductCollections 的数组的键是 "extra fields"

有人知道 JSON 应该如何修复这个错误吗?

如果尝试复制设置并检查 Twig 如何将数据发送到表单,如下所示:

Symfony\Component\HttpFoundation\Request {#7 ▼
  +attributes: Symfony\Component\HttpFoundation\ParameterBag {#10 ▶}
  +request: Symfony\Component\HttpFoundation\ParameterBag {#8 ▼
    #parameters: array:1 [▼
      "product_collection" => array:4 [▼
        "name" => "ewerew"
        "productSelections" => array:2 [▼
          0 => array:2 [▼
            "settings" => "qrew"
            "product" => "2"
          ]
          1 => array:2 [▼
            "settings" => "qwre"
            "product" => "1"
          ]
        ]
        "save" => ""
        "_token" => "z3um5Wu_iTZvr2OYsaGZJvtLBHVfJN5HYkO4UFQbpiw"
      ]
    ]
  }

它在 Twig 设置中保存得很好,所以我真的看不出我做错了什么....

所以我想知道以下几点:

尝试将 allow_add 属性添加到表单中的 CollectionType,因为它默认为 false。 Documentation

->add('productProductCollections', CollectionType::class, [
    'entry_type' => ProductProductCollectionType::class,
    'allow_add' => true,
])

您还可以在表单默认设置中将 allow_extra_fields 设置为 true。但是你不应该使用它,因为它允许 all 额外的字段。

$resolver->setDefaults([
    'data_class' => ProductCollection::class,
    'csrf_protection' => false,
    'allow_extra_fields' => true,
]);