@cali_green
To create an abstract type with different objects in Symfony, you need to follow these steps:
Here is an example of how the code might look:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
use SymfonyComponentFormAbstractType; use SymfonyComponentFormExtensionCoreTypeTextType; use SymfonyComponentOptionsResolverOptionsResolver; abstract class AbstractObjectType extends AbstractType { public function configureOptions(OptionsResolver $resolver) { $resolver->setDefaults([ 'class' => null, // Specific option for each object form ]); } public function getParent() { return TextType::class; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
use SymfonyComponentOptionsResolverOptionsResolver; class Object1Type extends AbstractObjectType { public function configureOptions(OptionsResolver $resolver) { parent::configureOptions($resolver); $resolver->setDefaults([ 'class' => Object1::class, ]); } public function getBlockPrefix() { return 'object1'; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
use SymfonyComponentOptionsResolverOptionsResolver; class Object2Type extends AbstractObjectType { public function configureOptions(OptionsResolver $resolver) { parent::configureOptions($resolver); $resolver->setDefaults([ 'class' => Object2::class, ]); } public function getBlockPrefix() { return 'object2'; } } |
Now, you can use the Object1Type
and Object2Type
form types in your forms, which will inherit the basic behavior defined in the AbstractObjectType
, while allowing you to configure specific options for each object type.