Symfony2 multiple Entities of same class in one Form

前端 未结 2 2088
天涯浪人
天涯浪人 2021-01-15 00:37

I want to render a form which has multiple Entities of same Class. I will display 2 fields, Price(type=text) and Enabled(type=checkbox).

I don\'t know how many I wil

2条回答
  •  一向
    一向 (楼主)
    2021-01-15 01:31

    I found the solution,

    the way I was creating the form in Controller was wrong! I had to do the following:

    $pricingForm = $this->createFormBuilder(array('prices'=>$prices))
                    ->add('prices','collection',array(
                        'required'       => true,
                        'allow_add'      => true,
                        'type'           => new PricingType(),
                   ))
                    ->getForm()
                ;
    

    "allow_add => true" is necessary when working with collection, otherwise it will NOT add any of PricingType collection of entities to the form.

    Then, because the form is built inside the controller "$this->createFormBuilder(array('prices'=>$prices))" , $prices array must be passed as an array with array keyname same as the one used in "->add('prices','collection',array(...)" , which is 'prices' so Symfony will know what to bind where. $prices is an array of Pricing objects array(0 => new Pricing()).

    In my PricingType I have:

    class PricingType extends AbstractType {
    
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
    
            $builder
                ->add('price', 'text', array(
                    'label' => false,
                    'required' => true
                ))
                ->add('enabled','checkbox',array(
                    'label'     => 'Use this currency',
    
                ))
            ;
    
        }
    
        public function setDefaultOptions(OptionsResolverInterface $resolver)
        {
            $resolver->setDefaults(array(
                'data_class'        =>  'XXX\XXX\Entity\Pricing',
                'csrf_protection'   => false
            ));
        }
    
        public function getName()
        {
            return 'pricingtype';
        }
    }
    

    Here I need to have control over label attribute. I could not find the way for it( if anyone knows please post how to). I override my twig template as follows:

    On the top we need next line of code:

        {% form_theme form_pricing _self %}
    

    Then override row and widget as follows (it was a nightmare to debug):

    {% block _form_prices_entry_row %}
        {% spaceless %}
            {{ form_widget(form) }}
        {% endspaceless %}
    {% endblock %}
    
    {% block _form_prices_entry_widget %}
        {% spaceless %}
    
            {{ form_row(form.price, { 'label' : form.vars.value.getCurrency().getTitle() } ) }}
            {{ form_row(form.enabled) }}
    
        {% endspaceless %}
    {% endblock %}
    

    In the body then, render form elements as follows:

    {% for price in form_pricing.prices %}
                        
    {{ form_row(price) }}
    {% endfor %}

    I really hope this will help you guyz. It was a real nightmare to debug especially the twig file, I did it thanks to my clever colleague.

提交回复
热议问题