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

Access underlying data in the callback #4

Open
aesislabs opened this issue Sep 11, 2023 · 3 comments
Open

Access underlying data in the callback #4

aesislabs opened this issue Sep 11, 2023 · 3 comments

Comments

@aesislabs
Copy link

Hi,

I have a very complex form with multi interdependent fields and in some of them I need to get the main form's underlying data, is there a way to do this ?

fransweerts pushed a commit to fransweerts/dynamic-forms that referenced this issue Sep 12, 2023
…is not compatible with Symfony\Component\Form\FormBuilderInterface::add
@weaverryan
Copy link
Contributor

Hi!

Which data do you need access to exactly? What I mean is, suppose you are editing a BlogPost entity that you query from the database. In your callback, do you simply need to (A) access how the BlogPost object looked when it originally came from the database? Or (B) do you need to see what it looked like after all the data is submitted on it?

For (A), you can, I think, just grab the $data key from $options:

public function buildForm(FormBuilderInterface $builder, array $options): void
{
    $builder = new DynamicFormBuilder($builder);
    // ...
    $data = $options['data'] ?? null;

    $builder->addDependent('mainFood', ['meal'], function(DependentField $field, string $meal) use ($data) {
        // ...
    });

For (B), I'm not sure if that's possible or not... I think (?) by the time we have let ALL of the fields submit their data and update the underlying object, it is now too late for us to add new fields. I could be wrong - these form events are complicated, even for me (hence creating the library so we all don't need to think about them anymore).

@aesislabs
Copy link
Author

I want to access the global underlying object as it is.

For example, in my custom dependent fields implementation, for some choice type I pass the "data" object, it allows me to populate the choice with some fields from the same global form and update it dynamically.

For example on a form, I have a collection of "people" (name, surname) and I use this collection to populate another choice with theses names and it updates dynamically

Thanks !

@weaverryan
Copy link
Contributor

Does solution (A) work for you then? Or, if not, if you have a collection of "people" in your form and you use that to populate another choice, then wouldn't you use ->addDependent('thatOtherChoiceField', ['people'], ...) so that the data from the people field is passed to you? Or is this people collection not actually part of your form? And, if that's true, then I think (A) should work?

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