I’d like to know if I can generate forms dynamically using a select, depending of its value, here’s the code to explain.
It is possible to generate between 1 and 5 RelationType form depending of the value of nombreLiens ?
<?php
namespace AppForm;
use SymfonyComponentFormAbstractType;
use SymfonyComponentFormFormBuilderInterface;
use SymfonyComponentOptionsResolverOptionsResolver;
use SymfonyBridgeDoctrineFormTypeEntityType;
use DoctrineORMEntityRepository;
use SymfonyComponentFormCallbackTransformer;
use DoctrineORMEntityManagerInterface;
use SymfonyComponentFormExtensionCoreTypeCollectionType;
use SymfonyComponentFormExtensionCoreTypeTextareaType;
use SymfonyComponentFormExtensionCoreTypeHiddenType;
use SymfonyComponentFormExtensionCoreTypeChoiceType;
class RelationCollectionType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder->add('relations', CollectionType::class, [
'entry_type' => RelationType::class,
'allow_add' =>true,
'by_reference' => false,
'allow_delete' => true
])
->add('identite', HiddenType::class)
->add('entite', HiddenType::class)
->add('nombreLiens', ChoiceType::class, [
'choices' => [
'1' => 1,
'2' => 2,
'3' => 3,
'4' => 4,
'5' => 5,
],
'mapped' => false,
'label' => 'Nombre de liens',
]);
}
}