I've got a big problem. I want to pass two entities to the form builder, as I want one form to create client and user account at once. Everything is ok until I'm trying to process the post data. When I add validation, it fails + there is no data in new entities. Seems like there is a problem with binding the form to entities, but I don't know why. My form and action look like this:
form
- Code: Select all
class RegisterUser extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
$builder->add('username', 'text', array('label' => 'Nazwa użytkownika*')) //from User entity
->add('password', 'password', array('label' => 'Hasło*')) //from User entity
->add('email', 'text', array('label' => 'Adres e-mail*')) //from User entity
->add('companyName', 'text', array('label' => 'Nazwa firmy*')) //from Client entity
->add('companyAddress', 'text', array('label' => 'Adres firmowy')) //from Client entity
->add('companyPhone', 'text', array('label' => 'Telefon firmowy')) //from Client entity
->add('type', 'entity', array('class' => 'CoreUserBundle:Type', 'label' => 'Sektor działalności')); //from Client entity
return $builder->getForm();
}
public function getName()
{
return 'registerUser';
}
}
registerAction
- Code: Select all
public function registerAction() {
$newUser = new \Core\UserBundle\Entity\User();
$newClient = new \Core\UserBundle\Entity\Client();
//here I'm passing two entities in array to the formBuilder, and I have access to the fields in the builder, everything seems to be ok
$form = $this->createForm(new \Core\AuthBundle\Form\Registration\RegisterUser(), array($newUser, $newClient));
$request = $this->getRequest();
if ($request->getMethod() == 'POST') {
$form->bindRequest($request);
if ($form->isValid()) {
// but when I'm setting validation, it fails. Seems like there is a problem with binding the form to entities, but I don't know why. I guess it has a problem as I passed two entities in array, and not one
//if I'd try to echo $newUser->getUsername() here, it's empty.
}
}
return $this->render('CoreAuthBundle:Default:registration.html.twig', array('form' => $form->createView()));
}
Can somebody help me on that one? I really don't know why it doesn't want to bind my post data correctly ;/ When I'm passing only one entity to the form, everything seems to be fine.
