Quantcast
Channel: Active questions tagged symfony4 - Stack Overflow
Viewing all articles
Browse latest Browse all 3925

How to pass data into CollectionType form and get it there?

$
0
0

How can I get in correct way data which send into a collection?

Important:

If I delete from the Create Form class this line:

'data'=> array_values($builder->getData()->cpus), //TODO: shit code send data And add in Collection 

And add in Collection Form class this:

'data_class' => Command::class, //namespace (Collection\Command)

This works only in view (twig) stage, but firstly I have to get those data before creating collection form (view).

How can I do that?

At this moment I found this solution (which looks "shitty") :). Lines marked as TODO

Collection\Command:

class Command{    public $id_item;    /**     * @var ItemInterface $interface     */    public $interface;    public $id_object;    public function __construct(string $id_item, ItemInterface $interface)    {        $this->id_item = $id_item;        $this->interface = $interface;    }    public function getIdItem(): string    {        return $this->id_item;    }    public function getInterface(): ItemInterface    {        return $this->interface;    }}

There is the problem, I can't get data from $cpus in good way :)

Collection\Form:

class Form extends AbstractType{    private $cpuModelFetcher;    public function __construct(CpuModelFetcher $cpuModelFetcher)    {        $this->cpuModelFetcher = $cpuModelFetcher;    }    public function buildForm(FormBuilderInterface $builder, array $options): void    {        $index = (int)$builder->getName(); //TODO: shit code - use name of form as index.        /**         * @var Command $command         */        $command = $builder->getData()[$index]; //TODO: shit code get data from array        $builder            ->add('id_object', Type\ChoiceType::class,                ['choices' => array_flip($this->cpuModelFetcher->getSupportedCpuModelListForModelBasedDeviceBySocketType(                        $command->getIdItem(),$command->getInterface()->getId())),'placeholder' => 'not installed in '.$command->getInterface()->getName().' socket.','required' => false,                ]);    }    public function configureOptions(OptionsResolver $resolver): void    {        $resolver->setDefaults(array(            //'data_class' => Command::class,'data_class' => null,        ));    }}

Create Command:

class Command{    /**     * @Assert\NotBlank()     */    private $id;    public $cpus;    private function __construct(Id $id)    {        $this->id = $id;    }    public static function fromDevice(Device $device): self    {        $command = new self($device->getId());        if($device->getBasedType()->isModelBasedDevice()){            $command->cpus = $command->transformDeviceModelInterfaceCollectionToCommandArray($device->getId(),                $device->getModelBasedDevice()->getDeviceModel()->getCpuInterfaces());        }        return $command;    }    private function transformDeviceModelInterfaceCollectionToCommandArray(Id $id_item, ArrayCollection $interfaces): array    {        $object_specific_interfaces = [];        foreach ($interfaces as $one){            /**             * @var DeviceModelInterface $one             */            for($i=0; $i<$one->getAmount(); $i++){                $object_specific_interfaces[] = new Collection\Command($id_item->getValue(), $one->getInterface());            }        }        unset($one);        return $object_specific_interfaces;    }}

Create Form:

class Form extends AbstractType{    public function buildForm(FormBuilderInterface $builder, array $options): void    {        //dump($builder);        $builder            ->add('cpus', CollectionType::class, ['label' => false,'entry_type' => Collection\Form::class,'entry_options' => ['label' => false,'data'=> array_values($builder->getData()->cpus), //TODO: shit code send data                ],'by_reference' => true,'allow_add' => false,'allow_delete' => false,'delete_empty' => true,            ])            ->add('save', Type\SubmitType::class, ['attr' => ['class' => 'btn btn-primary'],            ]);    }    public function configureOptions(OptionsResolver $resolver): void    {        $resolver->setDefaults(array('data_class' => Command::class,        ));    }}

Thank you for any help.


Viewing all articles
Browse latest Browse all 3925

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>