Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
//..
function editAction(){
$form = $this->createForm(new Forms\UserType($userConfig), $user);
$form->handleRequest($request);
if($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($userConfig);
$em->persist($user);
$em->flush();
}
}
<?php
//..
class UserType(){
private $userConfig = null;
public function __construct($userConfig = null) {
$this->userConfig = $userConfig;
}
public function buildForm(FormBuilderInterface $builder, array $options) {
//...
$builder->add('userConfig', new UserConfigPartialType(), array(
'label' => false,
'required' => false,
'mapped' => false,
'data' => $this->userConfig
));
//...
}
}
@webdevilopers

This comment has been minimized.

Copy link

webdevilopers commented Dec 22, 2014

The idea I mentioned on yout blog
http://willbradley.name/2014/08/26/two-unrelated-no-association-entities-in-one-form-in-symfony2/#comment-2457
was just another way to pass custom data to a form.

Instead of using setter injection you can use the defaultOptions - since this is what they were designed for.

$user = array('data' = $userConfigData);
$form = $this->createForm(new Forms\UserType($userConfig), $user);
class UserType()
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('userConfig', new UserConfigPartialType(), array(
            'label' => false,
            'required' => false,
            'mapped' => false,
            'data' => $options['data']
        )); 
    }

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

But this is just something in general.

I think you are simply trying to populate the form, right?

Then you should be able to simply use:

$data = array(
    'userConfig' => array(
        // name of your form partial type filled with the data you want
    )
);
$form = $this->createForm(new Forms\UserType());
$form->setData($data);

Untested.

@zyphlar

This comment has been minimized.

Copy link
Owner Author

zyphlar commented Dec 23, 2014

Thanks, but the purpose of the form is to insert and process two entities into a form and sub-form... so wouldn't overwriting data cause $user to be ignored in favor of $userConfig?

Consider the use-case of a single form containing both user account information and some other entity that can't be accessed directly via the user entity, like "Favorite Color".

@webdevilopers

This comment has been minimized.

Copy link

webdevilopers commented Dec 23, 2014

Sure, data is a bad defaultOptions name in the example above. It should be something different.

But talking about populating the form data you would have to add all the data you want to get set into the $data variable.

So if you need a user and a favoriteColor though the (sub-)forms / data-class Entities are not associated this should work:

$data = array(
    'userAccount' => array(
        // populate form 1
    ),
    'favoriteColor' => array(
        // populate form 2
    )
);
$form = $this->createForm(new Forms\UserType());
$form->setData($data);
@zyphlar

This comment has been minimized.

Copy link
Owner Author

zyphlar commented Dec 23, 2014

Great, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.