Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4618882
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:21:01+00:00 2026-05-22T02:21:01+00:00

I have an entity User and an entity Address. There is a relation One-to-Many

  • 0

I have an entity User and an entity Address. There is a relation One-to-Many between User and Address :

    class User
    {
        /**
        * @orm:OneToMany(targetEntity="Address")
        */
        protected $adresses;

        [...]

    }

I have a class AddressType, and class UserType :

    class UserType extends AbstractType
    {
        public function buildForm(FormBuilder $builder, array $options)
        {
            $builder->add('addresses', 'collection', array('type' => new AddressType()));

        }

        [...]
    }

In my controller, I build form with :

    $form = $this->get('form.factory')->create(new UserType()); 

… and create view with :

    return array('form' => $form->createView());

I display form field in my twig template with :

    {{ form_errors(form.name) }}
    {{ form_label(form.name) }}
    {{ form_widget(form.name) }}
    [...]

Okay. Now, how to display fields for one or more addresses ? (it’s no {{ for_widget(form.adresses.zipcode) }} nor {{ for_widget(form.adresses[0].zipcode) }} …)

Any ideas ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T02:21:01+00:00Added an answer on May 22, 2026 at 2:21 am

    This is how I did it in my form template:

    {{ form_errors(form.addresses) }}
    
    {% for address in form.addresses %}
        <div id="{{ 'address%sDivId'|format(loop.index) }}" class="userAddressItem">
            <h5> Address #{{ loop.index }}</h5>
    
            {{ form_errors(address) }}
            {{ form_widget(address) }}
        </div>
    {% endfor %}
    

    And I have a small action bar, driven by jQuery, that lets the user add and remove addresses. It is a simple script appending a new div to the container with the right HTML code. For the HTML, I just used the same output has Symfony but with updated index. For example, this would be the output for the street input text of the AddressType form:

    <input id="user_addresses_0_street" name="user[addresses][0][street]" ...>

    Then, the next index Symfony will accept is 1 so the new input field you add would look like this:

    <input id="user_addresses_1_street" name="user[addresses][1][street]" ...>

    Note: The three dots are a remplacement for required="required" maxlength="255" but could change depending on your needs.

    You will need more HTML code than that to add a whole new AddressType to the DOM of the browser but this give you the general idea.

    Regards,
    Matt

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

it's possible to have one entity object (class, e.g. User) for more entity models
I have an entity loaded by Hibernate (via EntityManager ): User u = em.load(User.class,
We have an JPA @Entity class (say User) which has a @ManyToOne reference (say
I have Doctrine 2.1 entity: /** * @Entity(repositoryClass=Varlab\Model\Repository\UserRepository) * @Table(name=user) */ class User {
Let's say I have a User Entity, and created partial User class so I
Let's say you have a domain entity User and you want to support the
In our application, I have seen code written like this: User.java (User entity) public
I have the two entity classes, User and MyCharacter. User has a list of
I have entity model like this (using EclipseLink and JPA 2.0): @Entity class A
In my business model I have Entity class (IPoint interface ) that has a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.