如何使用FOSRest和Symfony表单

时间:2017-04-27 04:45:50

标签: php rest symfony fosrestbundle

使用Symfony 3.2和FOS REST Bundle我已经为资源创建了一些REST端点,并使用Symfony Form来定义API DOC的字段。到目前为止,一切都很好。 现在,我正在尝试改进我的架构,并在我的资源中添加了一个子实体(一对一)。我希望主要资源保存子实体 - 子实体没有专用端点。

我按照the Symfony documentation上的说明删除了所有其他字段以隔离任何问题。

这就是我的表单类型现在的样子:

<?php

namespace VendorName\MyBundle\Form;

use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class CountryType extends AbstractType
{
    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {

        $builder
            ->add('mySubEntity', EntityType::class, array(
                'multiple' => false,
                'expanded' => false,
                'property' => 'name',
                'class' => 'MyBundle\Entity\mySubEntity'));
    }

    /**
     * {@inheritdoc}
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'VendorName\MyBundle\Entity\Country',
            'csrf_protection' => false
        ));
    }

}

现在,当我加载api-docs时,收到错误消息The option "property" does not exist. Defined options are: "action", "allow_extra_fields", [...]

说实话,我甚至不知道在表单中添加实体是否是将其显示在API文档中的正确方法。任何帮助解决上述问题和/或实现这一目标的最佳实践将不胜感激。

编辑:感谢@miikes,此错误现已解决,我可以看到api doc正确显示嵌套表单的字段。但是,现在我的问题是表单没有填充父实体上的子实体。这似乎与我建立父子关系的方式有关,我发布了new question for this issue

1 个答案:

答案 0 :(得分:1)

要解决您的错误,请尝试使用choice_label,而不是property选项。

'choice_label' => 'name'

但是参考文档EntityType is a kind of ChoiceType,所以使用此类型,您只能选择现有实体,而不是保留新实体。

创建新实体实例的最简单,最明确的方法是创建另一个为您的实体设计的类型类,并将该类型作为字段添加到 CountryType

class MyEntityType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('some-field', TextType::class)
            ->add('another-field', TextType::class);
    }

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

class CountryType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('mySubEntity', MyEntityType::class);
    }

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

因此您应该将表单数据传递为

['mySubEntity']['some-field'] = 'foo'
['mySubEntity']['another-field'] = 'bar'

另一个提示是使用Country::class而不是字符串'VendorName\MyBundle\Entity\Country',因为在重命名类的情况下,IDE重构会影响您的类型。

相关问题