Hej, mam pewien problem z CollectionType.
Mianowicie, chciałem skorzystac z CollectionType + DataTransformer
class EditPostType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('type', null, [
'disabled' => true
])
->add('kind', null, [
'disabled' => true
])
->add('title', TitleTextType::class)
->add('description', DescriptionTextType::class)
->add('attributes', AttributesCollectionType::class)
->add('save', SubmitType::class)
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Post::class,
]);
}
}
AttributesCollectionType:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->addModelTransformer(
new AttributesCollectionTransformer()
);
}
public function getParent()
{
return CollectionType::class;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'allow_add' => true,
//'data_class' => Attributes::class
]);
}
AttributesCollectionTransformer:
public function transform($attributes)
{
$values = [];
/** @var Attributes $attribute */
foreach ($attributes as $attribute) {
$type = $attribute->getType();
switch ($type) {
case 'TYPE_TEXT':
$values[] = $attribute->getValue();
break;
case 'TYPE_SELECT':
$values[] = ''; //TODO - jak wyswietlic ChoiceType
break;
}
}
return $values;
}
Chciałem wyświetlać listę Atrybutów za pomocą collectionType - tylko pytanie w jaki sposób mogę zrobić„ to tak aby w zależności od potrzeb moje pole miało inny typ?