Replies: 1 comment
-
|
You cant pass a callable to In your // OptionType.php
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormEvent;
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event) {
$option = $event->getData(); // This is your SpecificationOption object
$form = $event->getForm();
if ($option && $myCondition) {
$form->setDisabled(true);
}
});
// Add your fields normally
$builder->add('someField', TextType::class);
}The key is that If you need to access something from outside (like a service or parent form data), you can pass it through $builder->add('options', CollectionType::class, [
'entry_type' => OptionType::class,
'entry_options' => [
'some_context' => $someValue,
],
]);Then in public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'some_context' => null,
]);
}
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$context = $options['some_context'];
$builder->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event) use ($context) {
$option = $event->getData();
// Use both $option and $context to make decisions
});
}This is the standard Symfony way to handle per-entry dynamic behavior in collections. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hello,
I am trying to have a CollectionType of OptionType as entry_type.
Each OptionType can have different attributes, for example OptionType 1 will be disabled but OptionType 2 will not.
I thought I could do this with entry_options by passing it a callable as you can do with choice_label.
(Like this)
But entry_options only allows arrays. Since I don't have access to the data without going through an EventListener in my OptionType, I can't do
$builder->setDisabled(true);I feel like I want to do something fairly basic, but I can't find any viable solutions.
I absolutely want my entire OptionType to be disabled, not just a few fields in my OptionType, so replacing the OptionType fields with option disabled set to true is not viable for me. And I don't want to add disabled attribute directly on twig template.
I would love to hear your ideas on this subject.
Thank you in advance.
Beta Was this translation helpful? Give feedback.
All reactions