0

Symfony version: 3

I need to create a form that insert user data into two tables. So I am following this method in the Symfony doc. I have two entities called Users and Address and I have created two form builders like below,

AddressType

class AddressType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('address');
        $builder->add('postCode');
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'PIE10Bundle\Entity\Address',
        ));
    }
 }

UserType

class UserType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('firstname');
        $builder->add('lastname');

        $builder->add('address', 
                      CollectionType::class, 
                      array(
                            'entry_type' => AddressType::class
         ));

        $builder->add('Add User', SubmitType::class);
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'PIE10Bundle\Entity\Users',

        ));
    }
}

and my Contoller,

public function addNewUserAction(Request $request)
{
    $user    = new Users;
    $address = new Address;

    $form = $this->createForm(UserType::class, $user);

    $form->handleRequest($request);

    return $this->render('PIE10Bundle:Form:newuser.html.twig',
                         array(
                                'title' => 'Add New User',
                                'form'  => $form->createView()
    ));
}

and yes finally the view PIE10Bundle:Form:newuser.html.twig

{% extends "PIE10Bundle::layout.html.twig" %}
{% block cmi_body %}
<div class="row">
    <div class="col-md-2">

    </div>
    <div class="col-md-6">
        {{form_start(form)}}
            {{form_widget(form)}}
        {{form_end(form)}}
    </div>
</div>
<div class="row">
    {{ dump(form) }}
</div>
{% endblock %}

And I am getting something like the below. enter image description here

in the form I am not getting the text fields for following

$builder->add('address');
$builder->add('postCode');

I need to know if I am missing something in my code or the reason for not getting the two text fields and how to fix this issue.

Thanks in advance.

2
  • Is this a 1 to 1 relationship between user and address? You might want to show your mapping. How you have your relationship set up between those two entities would determine which form field type to use. Commented Sep 8, 2016 at 11:04
  • yes this is 1 to 1 relationship Commented Sep 8, 2016 at 11:06

3 Answers 3

1

In your User class:

change:

$builder->add('address', 
     CollectionType::class, 
     array(
           'entry_type' => AddressType::class,array('label' => false)
     ));

to:

$builder->add('address', AddressType::class);

CollectionType is used when your relationship is one-to-many or many-to-many. So if your user was able to have multiple addresses, you would use CollectionType (but with some modification to your code).

UPDATE to deal with multiple labels:
You have two Address labels because when you add the formtype AddressType in your UserForm it takes on the label "Address" by default. Also inside your AddressType you have a field for "Address" which takes on the label "Address" by default, hence the two labels. Look at the code above. I've added array('label' => false) to the AddressType which will remove the first instance and leave the label on the actual field.

Sign up to request clarification or add additional context in comments.

6 Comments

I think he wants to see AddressType form fields in the view. Your way able to save only one address data.
See his comments. It is a 1 to 1 relationship, so he shouldn't be using CollectionType. Using the custom form type will get it into the view because he's rendering the entire form. When using CollectionType you have to set up 'allow_add', 'allow_delete' in order to add/remove items from the collection.
You you right about CollectionType, but he wants to see other Type fields in the same form. He should extend the AddressType from UserType.
You don't need to extend anything. the address entity has it's own form type. User has it's own form type. If the mapping is set up correctly, you add the address form to the user form with formbuilder as I've shown. The way he's rendering the form will render all the fields.
Yes :)) this works for me but I am getting extra "Address" in the form and you can see it here. the HTML for this extra address is like this <label class="required">Address</label> and are there any way to get rid of this extra label
|
1

You have to extend AddressType from UserType

Change your AddressType like that;

    class AddressType extends UserType
    {
        public function buildForm(FormBuilderInterface $builder, array         $options)
        {
            parent::buildForm($builder, $options);
            $builder->add('address');
            $builder->add('postCode');
        }

        public function configureOptions(OptionsResolver $resolver)
        {
            $resolver->setDefaults(array(
                'data_class' => 'PIE10Bundle\Entity\Address',
            ));
        }
     }

delete this part;

     $builder->add('address', 
                  CollectionType::class, 
                  array(
                        'entry_type' => AddressType::class
     ));

Comments

1

Inside the __construct of your user, add a new address instance

public function __construct()
{
    $this->address = new Address();
}

With that you will see the missing form fields

1 Comment

Did you mean in the Users Entity (which is the entity) or UserType (which is the FormType for Users Entity)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.