美好的一天。 我无法理解如何为CollectionType使用CallbackTransformer。 我用收集字段(SecondForm)编写FirstForm。 如何使用Callbacktransformer填写SecondForm?
FirstForm
<?php
namespace True\AdminBundle\Form\Type;
/* uses */
class FirstFormType extends AbstractType
{
public function buildForm (FormBuilderInterface $builder, array $options)
{
$builder->add('qwe', CollectionType::class, [
'label' => 'Any label',
'required' => false,
'prototype' => true,
'allow_add' => true,
'allow_delete' => true,
'mapped' => false,
'entry_type' => SecondCollectionFormType::class,
'entry_options' => array(
/* send some options to second form */
),
]);
$builder->get('qwe')
->addModelTransformer(new CallbackTransformer(
function ($tagsAsArray) {
// Problem HERE
$result['qwe'][] = ['logo' => '222', 'description' => '123'];
return $result;
},
function ($tagsAsString) {
$result['qwe'][] = ['logo' => '222', 'description' => '123'];
return $result;
}
))
;
$builder
->add('submit', SubmitType::class, [
'label' => 'Save',
])
;
}
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';
}
}