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

Field with multiple dependencies #25

Open
alcohol opened this issue May 27, 2024 · 2 comments
Open

Field with multiple dependencies #25

alcohol opened this issue May 27, 2024 · 2 comments

Comments

@alcohol
Copy link

alcohol commented May 27, 2024

I can't seem to get the following to work:

Form Foo with fields:

  • A / a checkbox type input
  • B / a choice type input
    • depends on A, if A is checked, B is not added
      ->addDependent('B', ['A'], function (DependentField $field, ?bool $checkbox): void {
        if ($checkbox) {
          return;
        }
        $field->add(ChoiceType::class, /* [...] */);
      }
  • C / a text type input
    • depends on A, if A is checked, C is not added, depends on B, options change based on the value of B
      ->addDependent('C', ['A', 'B'], function (DependentField $field, ?bool $checkbox, ?string $choice): void {
        if ($checkbox) {
          return;
        }
        $field->add(TextType::class, /* [...] */);
      }

The above doesn't work though; if I check A, B disappears, but C is still added (or not removed 🤷).

@alcohol
Copy link
Author

alcohol commented May 27, 2024

I guess this relates to #4 (comment) -- specifically, option B mentioned in this comment.

@toby-griffiths
Copy link

I have had a similar issue with a form for dietary requirements. I have the folllowing fields…

a. Do you have any dietrary requirements (yes/no)?
b. [only if a. = yes] Select your dietary requirement from a list of options;
c. [only if b. is not empty] Collection of fields asking…

  • What is the reason (choice/intolerance/allergy)?

The problem I had… if I go from a form with…
a. yes;
b. something slected (so c. contains some reason fields for the selection);
c completed reason field(s);
… to…
a. no;
… then field b. disappears, but c. does not… BUT… only when I save the data. If I don't perform a save, field c. also, correctly, disapears. Done a whole load of step debugging, but still cannot fathom why this might be!?

My workaround was to add the following event listener to the form builder that adds the dynamic fields…

        ->addEventListener(FormEvents::PRE_SUBMIT, function (PreSubmitEvent $event) {
            $data = $event->getData();
            if (!($data['hasRequirements'] ?? false)) { // <= if a. is 'no'
                // First I tried this, to remove the collection of reasons, however the previously selected
                // reasons form still appears, just with empty values…
                $data['requirements'] = [];
                // So I also removed the child form all together…
                $event->getForm()->remove('requirements');
            }
            $event->setData($data);
        });

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

2 participants