Forms 子窗体是否可以向父窗体而不是对象返回值?

Forms 子窗体是否可以向父窗体而不是对象返回值?,forms,symfony,Forms,Symfony,我看到Symfony2总是将表单类型绑定到对象上,但是否可以将表单类型重用到只能作为子级使用的位置 这只是我试图实现的一个示例,但我认为它可以应用于许多场景 首先,假设我有以下实体: class Person { public function getState(); public function getHomeState(); } 这两个字段是州的选择字段 我希望当用户选择一个选项时,getState()和getHomeState()应该只返回一个字符串 比如:

我看到Symfony2总是将表单类型绑定到对象上,但是否可以将表单类型重用到只能作为子级使用的位置

这只是我试图实现的一个示例,但我认为它可以应用于许多场景

首先,假设我有以下实体:

class Person
{
    public function getState();   
    public function getHomeState();   
}
这两个字段是州的选择字段

我希望当用户选择一个选项时,
getState()
getHomeState()
应该只返回一个字符串

比如:

<?php

namespace Acme\DemoBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;


class PersonFormType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('State', 'statelist'),//statelist is a form service.
            ->add('HomeState', 'statelist')//use form service here too
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Acme\DemoBundle\Entity\Person'
        ));
    }

    public function getName()
    {
        return 'person';
    }
}

是的,您可以将基本的“字符串”字段重新定义为“选项”,而无需添加内部字段,只需覆盖setDefaultOptions,下面是一个关于optionResolvePrinterface工作原理的很好的解释


但我认为这是另一个故事。需要了解的重要一点是,如果在formtype下添加字段,则字段将返回一个对象(或我不记得的数组),而不是字符串

谢谢!现在进入下一期…:)如果你想看看的话。
<?php

namespace Acme\DemoBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;

class StateListType extends AbstractType
{
    protected $ChoiceList;

    public function __construct(ChoiceListInterface $Choices)
    {
        $this->ChoiceList = $Choices;
    }
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        //Unsure what to do here to accomplish what I want. 
        $builder->add(?, 'choice', array('choice_list' => $this->ChoiceList);
    }

    public function getParent()
    {
        return 'choice';
    }

    public function getName()
    {
        return 'statelist';
    }
}
<?php

namespace Acme\DemoBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;

class StateListType extends AbstractType
{
    protected $ChoiceList;

    public function __construct(ChoiceListInterface $Choices)
    {
        $this->ChoiceList = $Choices;
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver
            ->setDefaults(
                array(
                    'choices' => $this->ChoiceList,
                    'empty_value' => 'select a state'
                )
            );

    }

    public function getParent()
    {
        return 'choice';
    }

    public function getName()
    {
        return 'statelist';
    }
}
/**
 * {@inheritdoc}
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->addViewTransformer($this->createTransformer());
}

/**
 * @return DataTransformerInterface
 */
protected function createTransformer()
{
    return new FieldTransformer($this->getRepository(), static::FIELD_NAME);
}