Symfony2: Form Collection of entities with one-to-one relationship

360 views Asked by At

I have 2 entities:

  1. TransactionDefaultParametersEntity

    • Parameter: widgetType
    • Parameter: defaultParameters (Has one to one relationship with DefaultParametersEntity)
  2. DefaultParametersEntity

    • Parameter: checkIn
    • Parameter: checkOut
    • Parameter: currency
    • Parameter: adults

The controller provides a collection of TransactionDefaultParametersEntities, and for each I want a form displayed with all DefaultParameterEntity parameters as well as the widgetType parameter provided by the TransactionDefaultParametersEntity.

Currently I have a form type for each entity:

  1. TransactionDefaultParametersFormType

    class DefaultSettingsFormType extends AbstractType
    {
    
    /**
     * @param  FormBuilderInterface $builder
     * @param  array                $options
     */
     public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('defaultParameters', 'collection', ['type' => default_parameters', 'allow_add' => true]);
    }
    
    public function configureOptions(OptionsResolver $resolver)
       {
        $resolver->setDefaults(array(
            'data_class' =>    'AppBundle\Entity\TransactionDefaultParametersEntity',
            'create' => false
           ));
       }
    
       public function getName()
       {
        return 'transaction_default_parameters';
       }
    }
    
  2. DefaultParametersFormType

    class DefaultParametersFormType extends AbstractType
    {
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
        $builder
            ->add('widgetType', 'choice', [
                'choices' => [
                    'Default',
                    'Booking Engine',
                    'Informative'
                ],
                'label' => 'Widget Type'
            ])
            ->add('checkIn', 'text', ['label' => 'Check In'])
            ->add('checkOut', 'text', ['label' => 'Check Out'])
            ->add('currency', 'text', ['label' => 'Currency'])
            ->add('adults', 'text', ['label' => 'adults']);
      }
    
      public function configureOptions(OptionsResolver $resolver)
      {
        $resolver->setDefaults(array(
            'data_class' => 'AppBundle\Entity\DefaultParametersEntity',
        ));
      }
    
      public function getName()
      {
        return 'default_parameters';
      }
    }
    

And lastly in the controller:

    // $transactionDefaultParametersCollection is a collection of TransactionDefaultParameterEntities
    $form = $this->createFormBuilder($transactionDefaultParametersCollection)
                    ->add('transactionDefaultParameters', 'transaction_default_settings')
                    ->add('save', 'submit', array('label' => 'Save'))
                    ->getForm();

    $form->handleRequest($request);

    if ($form->isValid()) {
        // do something
    }

    return $this->render(
        'settings/editDefaults.html.twig',
        [
            'form' => $form->createView()
        ]
    );

If this wasn't clear please let me know :). Also if I could be pointed to an article that would be very helpful. I looked at how to embed a collection of forms but this was a different case than the example provided.

0

There are 0 answers