M
M
maxeee522020-12-25 21:56:04
symfony
maxeee52, 2020-12-25 21:56:04

How to implement a product creation form with an attribute generator?

There are products, attributes and attribute values.

5fe634bb7875a648924621.jpeg

I want to create and edit goods using Forms.
In buildForm, I use CollectionType to be able to generate a pair of attributes (which are created in advance) - the value of the

Product Form:

public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name', TextType::class, [
            	'help' 			=> "Type product name",
        'required' 		=> TRUE,
      ])
            ->add('price', MoneyType::class, [
        'help' 			=> "Type product price",
        'divisor' 		=> 100,
        'required' 		=> TRUE,
      ])
      ->add('attributes', CollectionType::class, [
        'entry_type'	=> ProductAttributeValueType::class,
        'allow_add' 	=> true,
        'prototype' 	=> true,
      ])
    ;
    }


Pair form Atrbut - Value
public function buildForm(FormBuilderInterface $builder, array $options)
    {

        $builder
            ->add('name', TextType::class)
            ->add('product',ChoiceType::class, [
        'choices'		=> $this->getAttributeSelect(),
      ]) // Продукт (в процессе создания)
            ->add('attribute', ChoiceType::class, [
        'choices'		=> $this->getProductSelect(),
      ])
        ;
    }


The question is vague, but still.
Where should I move next?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
tukreb, 2020-12-25
@maxeee52

Then display it all in twig.
For example, in twig you can send the finished html form code CollectionTypeto the html data-prototype attribute

<div data-prototype="
{% apply escape %}
     {{ include('app/template/prototype/collection_form.html.twig', { 'form': form.attributes.vars.prototype }) }}
{% endapply %}"></div>

collection_form.html.twigsomething like this in the file :
{{ form_widget(form.name) }}
{{ form_widget(form.product) }}
{{ form_widget(form.attribute) }}

Next, through javascript, insert the html form from data-prototypewhen the user clicks the button to add a new attribute. When inserting, you will need to manually set indexes for new fields, by default symfony will put something like form[__name__]
Well, when saving, everything will be in the form of an array in attributes[]
If you already have attributes in attributes[], then simply foreachcreate the form through.
{% for row in form.attributes %}
    {{ form_widget(row.name) }}
    {{ form_widget(row.attribute) }}
{% endfor %}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question