简体   繁体   中英

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 ?

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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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