Answer the question
In order to leave comments, you need to log in
How to use DTO for CollectionType in symfony?
Good day everyone. I've run into a problem that I can't figure out.
There is a form, FirstFormType, on the form there is a CollectionType field referring to the second form. For the second form, you need to fill in the values through the DataTransformer. Below is a simplified code of two forms. Simplified to use CallbackTransformer`a instead of a full-fledged transformer. The problem is that the fields of the subform from the CollectionType are not filled -(
The first form
<?php
namespace True\AdminBundle\Form\Type;
/* uses */
class FirstFormType extends AbstractType
{
public function buildForm (FormBuilderInterface $builder, array $options)
{
$builder->add('qwe', CollectionType::class, [
'label' => 'asdasd',
'required' => false,
'prototype' => true,
'allow_add' => true,
'allow_delete' => true,
'mapped' => false,
'entry_type' => SecondCollectionFormType::class,
'entry_options' => array(
/*'fields' => $fields,
'descriptions' => $options['descriptions'][$key],*/
),
]);
$builder->get('qwe')
->addModelTransformer(new CallbackTransformer(
function ($tagsAsArray) {
// Проблемное место
$result['qwe'][] = ['logo' => '222', 'description' => '123'];
return $result;
},
function ($tagsAsString) {
$result['qwe'][] = ['logo' => '222', 'description' => '123'];
return $result;
}
))
;
$builder
->add('submit', SubmitType::class, [
'label' => 'Сохранить',
])
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'fields' => null,
'descriptions' => null,
'em' => null,
'template' => null,
'sectionName' => null,
'doctrine' => null,
'websiteId' => null,
));
}
public function getBlockPrefix()
{
return 'backend_form';
}
}
<?php
namespace True\AdminBundle\Form\Type;
/* uses */
class SecondCollectionFormType extends AbstractType
{
public function buildForm (FormBuilderInterface $builder, array $options)
{
$builder->add('logo', TextType::class, [
'label' => $label,
'required' => false,
'mapped' => false,
]);
$builder->add('description', TextareaType::class, [
'label' => $label,
'required' => false,
'mapped' => false,
]);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'fields' => null,
'descriptions' => null,
));
}
public function getBlockPrefix()
{
return 'backend_collection_form';
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question