Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dependent form not using submitted values with more than 1 dependency #14

Open
faizanakram99 opened this issue Dec 15, 2023 · 0 comments

Comments

@faizanakram99
Copy link

faizanakram99 commented Dec 15, 2023

First of all, thank you for this awesome library. Awesome work.

I don't have a reproducer, but here is an excerpt from the form type causing issues

foreach (CustomFieldDefinitionEntity::supportedModules() as $module) {

    // this works fine as there is just one dependency 'modules'

    $builder
        ->addDependent($module, 'modules', function (DependentField $field, ?array $modules) use ($module) {
            $insertionPoints = [...];

            $field->add(ChoiceType::class, [
                'choices' => $insertionPoints,
                'disabled' => !\in_array($module, $modules ?? [], true),
                'required' => true,
                'getter' => fn (CustomFieldDefinition $fieldDefinition) => $fieldDefinition->placement[$module] ?? "{$module}.default",
                'setter' => function (CustomFieldDefinition $fieldDefinition, ?string $value) use ($module): void {
                    $fieldDefinition->placement[$module] = StringFunctions::emptyStringToNull($value) ?? "{$module}.default";
                },
            ]);
        })
    ;
}

// this doesn't use submitted values of the child added above 
// (value-of<CustomFieldDefinitionEntity::supportedModules()>), 
// it uses default values (initial form values), 
// the only difference is that this has multiple dependencies 
// unlike above which is just dependent upon a single field.

foreach (CustomFieldDefinitionEntity::ALLOWED_PROPAGATIONS as $propagationSource => $propagationDestinations) {
    $builder
        ->addDependent("propagation_{$propagationSource}", CustomFieldDefinitionEntity::supportedModules(), function (DependentField $field, ...$modules) use ($builder, $propagationDestinations, $propagationSource): void {

            // `$modules` doesn't contain submitted data 

            $field->add(ChoiceType::class, [
                'multiple' => true,
                'choices' => [...],
                'choice_attr' => $choiceAttributes,
                'required' => false,
                'expanded' => true,
                'getter' => fn (CustomFieldDefinition $fieldDefinition) => $fieldDefinition->propagations[$propagationSource] ?? [],
                'setter' => function (
                    CustomFieldDefinition $fieldDefinition,
                    ?array $destinations,
                    FormInterface $form
                ) use ($propagationSource): void {
                    $fieldDefinition->propagations[$propagationSource] = $destinations ?? [];
                },
            ]);
        })
    ;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant