简体   繁体   English

将模型传递到Symfony2中的复合形式

[英]passing model to composite form in Symfony2

I have the following composite form and I wanted to pass in the location as a model/object to LocationType , how can I do the same thing inside the buildForm ? 我有以下复合形式,我想将位置作为模型/对象传递给LocationType ,如何在buildForm做同样的事情?

If this was a regular controller then I would do this: 如果这是一个常规控制器,那么我会这样做:

$registrationForm = $this->createForm(new ShopRegistrationFormType(), $location, array('label' => false, 'required' => false));

but how can I do the same thing in a FormType that I have below: 但是我该如何在FormType中执行以下操作:

class ShopRegistrationFormType extends AbstractType
{

    private $location;

    public function __construct( $location = null)
    {
        $this->location = $location;
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('location', new LocationType())
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'cascade_validation' => true,
        ));
    }

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

Try to add model/object via 'data' attribute: 尝试通过“数据”属性添加模型/对象:

class ShopRegistrationFormType extends AbstractType
{

    private $location;

    public function __construct( $location = null)
    {
        $this->location = $location;
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('location', new LocationType(), array(
            'data' => $this->location,
        ));
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'cascade_validation' => true,
        ));
    }

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

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM