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 7587579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:42:30+00:00 2026-05-30T19:42:30+00:00

all. I would like to ask if it is possible to have multiple forms

  • 0

all.

I would like to ask if it is possible to have multiple forms (now per select option) on one page instead of multiple select field.

The situation is: I have User with @ManyToMany bi-relation to Services and ‘user_services’ relation storage table, but extended with additional fields like min_price, max_price, etc. with UserService Doctrine Entity class.

I think that the better user experience in my particular case is to have a table layout with checkboxes, service names and price fields with one save button, but I can’t get how to create multiple forms in which each form corresponds to one option from select list for example and followed by additional fields for this option.

Thanks.

  • 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-30T19:42:31+00:00Added an answer on May 30, 2026 at 7:42 pm

    Imagine that you have a bunch of services and you want to display them

    enter image description here

    What would you do?

    My Solution:

    Controller:

    class DefaultController extends Controller
    {
        public function indexAction(Request $request)
        {
            /** @var $em \Doctrine\ORM\EntityManager */
            $em = $this->get('doctrine.orm.entity_manager');
            $services = $em->getRepository('ThestudioscheduleProfileBundlesServiceBundle:Service')->findAll();
            $user = $this->get('security.context')->getToken()->getUser();
            $form = $this->createForm(new UserServiceType($services, $user->getServiceDetails()));
            if ('POST' == $request->getMethod()) {
                $form->bindRequest($request);
                if ($form->isValid()) {
                    $data = $form->getData();
                    $em->getConnection()->beginTransaction();
                    try {
                        foreach ($data['services'] as $serviceDetails) {
                            // if service is selected
                            if ($serviceDetails['id']) {
                                $serviceDetails['details']->setUser($user);
                                $serviceDetails['details']->setService($em->getRepository('ThestudioscheduleProfileBundlesServiceBundle:Service')->find($serviceDetails['service']));
                                $serviceDetails['details'] = $em->merge($serviceDetails['details']);
                            } else {
                                // if the entity is exist but user unchecked it - delete it
                                if ($serviceDetails['details']->getId()) {
                                    $serviceDetails['details'] = $em->merge($serviceDetails['details']);
                                    $em->remove($serviceDetails['details']);
                                }
                            }
                        }
                        $em->flush();
                        $em->getConnection()->commit();
                        // TODO: display success message to user
                        return $this->redirect($this->generateUrl('ThestudioscheduleProfileBundlesServiceBundle_homepage'));
                    } catch (\Exception $e) {
                        $em->getConnection()->rollback();
                        $em->close();
                        var_export($e->getMessage());die;
                        // TODO: log exception
                        // TODO: display something to user about error
                    }
                }
            }
            return $this->render('ThestudioscheduleProfileBundlesServiceBundle:Default:index.html.twig', array(
                'form' => $form->createView()
            ));
        }
    }
    

    UserServiceType:

    class UserServiceType extends AbstractType
    {
        private $services;
        private $userServiceDetails;
        public function __construct($services, $userServiceDetails)
        {
            $this->services = $services;
            $this->userServiceDetails = $userServiceDetails;
        }
        /**
         * @param \Symfony\Component\Form\FormBuilder $builder
         * @param array $options
         * @return void
         */
        public function buildForm(FormBuilder $builder, array $options)
        {
            // all application services
            $builder->add('services', 'collection');
            foreach ($this->services as $key => $service) {
                $serviceType = new ServiceType($service, null);
                foreach ($this->userServiceDetails as $details) {
                    if ($service == $details->getService()) {
                        $serviceType->setUserServiceDetails($details);
                    }
                }
                $builder->get('services')->add('service_' . $key, $serviceType);
            }
        }
        /**
         * Returns the name of this type.
         *
         * @return string The name of this type
         */
        public function getName()
        {
            return 'profile_user_service';
        }
    }
    

    ServiceType:

    class ServiceType extends AbstractType
    {
        private $service;
        private $userServiceDetails;
        public function __construct($service, $userServiceDetails)
        {
            $this->service = $service;
            $this->userServiceDetails = $userServiceDetails;
        }
        /**
         * @param \Symfony\Component\Form\FormBuilder $builder
         * @param array $options
         * @return void
         */
        public function buildForm(FormBuilder $builder, array $options)
        {
            $builder->add('id', 'checkbox', array(
                        'label' => $this->service->getName(),
                        'required' => false))
                    ->add('service', 'hidden')
                    ->add('details', new ServiceDetailsType($this->userServiceDetails));
            $values = array('service' => $this->service->getId());
            if (null !== $this->userServiceDetails) {
                $values = array_merge($values, array('id' => true));
            }
            $builder->setData($values);
        }
        /**
         * Returns the name of this type.
         *
         * @return string The name of this type
         */
        public function getName()
        {
            return 'profile_service';
        }
        public function setUserServiceDetails($details)
        {
            $this->userServiceDetails = $details;
        }
    }
    

    ServiceDetailsType:

    class ServiceDetailsType extends AbstractType
    {
        private $details;
        public function __construct($details)
        {
            $this->details = $details;
        }
        /**
         * @param \Symfony\Component\Form\FormBuilder $builder
         * @param array $options
         * @return void
         */
        public function buildForm(FormBuilder $builder, array $options)
        {
            $builder->add('id', 'hidden')
                    ->add('minPrice', 'money', array('required' => false))
                    ->add('maxPrice', 'money', array('required' => false))
                    ->add('unit', 'choice', array(
                        'choices' => array(
                            'hour' => 'Hours',
                            'photo' => 'Photos'
                        ),
                        'required' => false
                    ))
                    ->add('unitsAmount', null, array('required' => false));
            if (!empty($this->details)) {
                $builder->setData($this->details);
            }
        }
        public function getDefaultOptions(array $options)
        {
            return array(
                'data_class' => 'Thestudioschedule\ProfileBundles\ServiceBundle\Entity\UserService',
                'csrf_protection' => false
            );
        }
        /**
         * Returns the name of this type.
         *
         * @return string The name of this type
         */
        function getName()
        {
            return 'profile_service_details';
        }
    }
    

    The most amazing thing that all this works! Many thanks to Florian for spending his time on me and for keeping me thinking about the solution and my apologise for unclear question (if it is). I think, Symfony docs should be updated with more different form embedding/collection examples like this.

    Cheers,
    Dima.

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

Sidebar

Related Questions

I would like to ask if it is at all possible to let MySQL
I have a DataGridView that I would like all values on a specific row
I have a blog installed in www.foo.com/wp/ and would like all requests that go
I would like to have all developers on my team to use the same
I would like to select all descendant but blog nodes. For the example, only
As much as we would all like to say it is a benefit to
I would like to wipe out all data for a specific kind in Google
We would like to enumerate all strings in a resource file in .NET (resx
I would like to search through all of my procedures packages and functions for
I would like to find all the rows in a table and match on

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.