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

[Feature idea] Allow multiple fields to be added at the same time #11

Open
bastien70 opened this issue Nov 19, 2023 · 0 comments
Open

Comments

@bastien70
Copy link

Hey! First of all, thank you for this bundle, it will revolutionize the symfony forms system in the long term, that's for sure!

In your opinion, would it be a good thing to allow the addition of several fields at the same time, depending on the same fields?

Here is a very simple example (which doesn't make sense):

    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder = new DynamicFormBuilder($builder);

        $builder
            ->add('category', EntityType::class, [
                'placeholder' => 'Choisir une catégorie',
                'query_builder' => function(CategoryRepository $categoryRepository): QueryBuilder {
                    return $categoryRepository->createQueryBuilder('c')
                        ->join('c.articles', 'a');
                },
                'class' => Category::class,
                'mapped' => false,
            ])
            ->addDependent('article', ['category'], function(DependentField $field, ?Category $category) {
                if($category)
                {
                    $field->add(EntityType::class, [
                        'placeholder' => 'Choisir un article',
                        'class' => Article::class,
                        'choices' => $category->getArticles(),
                    ]);
                }
            })
            ->addDependent('title', ['category', 'article'], function (DependentField $field, ?Category $category, ?Article $article) {
                if($category && $article)
                {
                    $field->add(TextType::class);
                }
            })
            ->addDependent('content', ['category', 'article'], function (DependentField $field, ?Category $category, ?Article $article) {
                if($category && $article)
                {
                    $field->add(TextareaType::class);
                }
            })
        ;
    }

We are in the form of a Comment.

To add a Comment, I must first select a category. Then an article from those in the chosen category.

Once these 2 fields are selected, we can finally enter the title and content of the Comment.

Here, we are forced to carry out the same procedure for the "title" and "content" fields, namely, check that "category" and "article" are not null.

Which may be redundant.

But we can imagine a case where depending on a user's choice, there would be around ten fields which should be displayed depending on choice A or choice B, and it would take a long time to have to do the same ->addDependent() for each field.

We could imagine something like this:

    public function buildForm2(FormBuilderInterface $builder, array $options): void
    {
        $builder = new DynamicFormBuilder($builder);

        $builder
            ->add('category', EntityType::class, [
                'placeholder' => 'Choisir une catégorie',
                'query_builder' => function(CategoryRepository $categoryRepository): QueryBuilder {
                    return $categoryRepository->createQueryBuilder('c')
                        ->join('c.articles', 'a');
                },
                'class' => Category::class,
                'mapped' => false,
            ])
            ->addDependent('article', ['category'], function(DependentField $field, ?Category $category) {
                if($category)
                {
                    $field->add(EntityType::class, [
                        'placeholder' => 'Choisir un article',
                        'class' => Article::class,
                        'choices' => $category->getArticles(),
                    ]);
                }
            })
            ->addListener(on: ['category', 'article'], function (DynamicFormBuilder $builder, ?Category $category, ?Article $article) {
                if($category && $article)
                {
                    $builder->add('title', TextType::class)
                        ->add('content', TextareaType::class);
                }
            })
        ;
    }

But maybe it is already more or less possible to do this kind of thing with the addEventListener method that you propose?

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