Created
December 19, 2014 18:47
-
-
Save zyphlar/b98e054526a3d0dc26e3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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 | |
| )); | |
| //... | |
| } | |
| } |
Author
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".
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);
Author
Great, thanks!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.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:
Untested.