phpformssymfony

CollectionType of EntityType Symfony Forms


It is even possible to make ?

->add('product', CollectionType::class, [
      'entry_type'    => EntityType::class, array(
        'data' => $options['product'],
        'placeholder' => 'Wybierz klienta',
        'multiple' => true,
        'class' => Products::class,
        'attr' => ['class' => 'chosen-select','data-placeholder'=>'Wybierz produkt'],
        'choice_label' => function ($product) {
            return  ''.$product->getJson()["products"]["name"] .' | Stan Magazynowy: '.$product->getJson()["products"]["stock"].'';
          },
      'label' => 'Wybierz produkty'

      ),
      'entry_options' => [
          'label' => 'Value',
      ],
      'label'        => 'Add, move, remove values and press Submit.',
      'allow_add'    => true,
      'allow_delete' => true,
      'prototype'    => true,
      'required'     => false,
      'attr'         => [
          'class' => 'my-selector',
      ],
  ])

I'll try to add chosen list of Products::class in CollectiontType, if some one wanna add product to new order, can add new EntityType and select product, and after submit i`ll handle this as array and save it to order.

If someone has another idea how to add products to form with quantity and then post it to array, please free to write :)


Solution

  • I think this is not the best way to do that. You just have to add CollectionType to the FormType and update you ProductType class to handle if this is selected or not. May be you have to create a custom formtype for products for order only :-/ . All hard work are on the javascript side