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

[Form] EntityType: 'query_builder' and 'setMaxResults' weird behavior #31559

Closed
Adrien-H opened this issue May 20, 2019 · 5 comments
Closed

[Form] EntityType: 'query_builder' and 'setMaxResults' weird behavior #31559

Adrien-H opened this issue May 20, 2019 · 5 comments

Comments

@Adrien-H
Copy link

Symfony version(s) affected: 4.2.7

Description
I'm not sure this is a bug. It might be a simple question, but surely about a behavior I consider like weird.
In some Form, in some EntityType with 'multiple' => true, if you interfer with the query_builder and call setMaxResults(x) on the builder, then x will also enter in validation flow: the form field will be invalid if the number of submitted entities is greater than x.

How to reproduce
Let's consider this:

public function buildForm(FormBuilderInterface $builder, array $options): void
{
    $builder
        ->add('authors', EntityType::class, [
            'required' => false,
            'class' => User::class,
            'query_builder' => function (EntityRepository $r) {

            return $r
                ->createQueryBuilder('user')
                ->setMaxResults(2)
            ;
        },
   ;
}

From this point, nothing stops the frontend from sending more than 2 entities. You could imagine Select2 field with its own AJAX request. And this will lead to the form being invalid.

One could argue this makes no sense to limit the query to 2 results then sending 3 entities. While I could agree with this, my concern is not about the logic behind the behavior, but the fact that I think it's not the role of the query_builder option to define parallel constraints on its way or have any influence upon form validation. It is not documented, it raises very generic error messages, in one word it is rather unpredictable.

Possible Solution
If it's a bug, well it's a bug, happy for having reported it. I did not have any time to dig into the code and understand the behavior, though, so I did not come out with a fix. I could try finding some time for this if you ask. (:

If it's a wanted behavior, I won't debate the choice itself, but I think it could be better documented or at least raise a more accurate error message on the form field.

@xabbuh
Copy link
Member

xabbuh commented May 21, 2019

The EntityType extends the ChoiceType. Its semantic is that your provide a list of choices to the user from which the user chooses one or multiple. Every submitted value that is not part of this choice list of treated being invalid. This is the expected behaviour of the ChoiceType and thus the expected behaviour of all its concrete subtypes too. So I am going to close here as there is no bug. Thank you for understanding.

@Adrien-H
Copy link
Author

The issue I faced was just concerning the LIMIT. If I setMaxResults(3), still can I select values that are not in the original result. I just cannot select more than 3 values.

While I understand the semantic you describe of "You queryied a b c, so pick what you want of a b c", this is not the behavior I am observing. It's more like "Your queryied a b c, so pick whatever you want but no more than 3."

But I respect your point, this is revealed anyway by a bad usage of EntityType. Thank you for your answer, have a good day. (:

@xabbuh
Copy link
Member

xabbuh commented May 21, 2019

Sorry, I did not understand your description that way as your example was talking about a limit of two but a submission of three values. This looks like a bug to me then.

@xabbuh xabbuh reopened this May 21, 2019
@Adrien-H
Copy link
Author

Okay, yeah, sorry, I probably lacked precision in my first post. I was not aware of the ChoiceType behavior you described.

I will try to find time this week to investigate.

@HeahDude
Copy link
Contributor

HeahDude commented Dec 9, 2019

Thank you @Adrien-H for reporting this issue, I was able to work on a fix and opened #34900.

The problem is that we try to optimize the query on submission by adding a WHERE ... IN (...) clause to the builder define in the option, which is used before the limit clause to fetch the results.

This is an edge case where we should not try to optimize the loading since we cannot fix it on the db layer, and given that is about a "limit" clause this should not be a big deal hopefully.

@fabpot fabpot closed this as completed Dec 15, 2019
fabpot added a commit that referenced this issue Dec 15, 2019
…ueries with limit (HeahDude)

This PR was merged into the 3.4 branch.

Discussion
----------

[DoctrineBridge] Fixed submitting invalid ids when using queries with limit

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no <!-- please update src/**/CHANGELOG.md files -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tickets       | Fix #31559 <!-- prefix each issue number with "Fix #", if any -->
| License       | MIT
| Doc PR        | ~
<!--
Replace this notice by a short README for your feature/bugfix. This will help people
understand your PR and can be used as a start for the documentation.

Additionally (see https://symfony.com/roadmap):
 - Always add tests and ensure they pass.
 - Never break backward compatibility (see https://symfony.com/bc).
 - Bug fixes must be submitted against the lowest maintained branch where they apply
   (lowest branches are regularly merged to upper ones so they get the fixes too.)
 - Features and deprecations must be submitted against branch master.
-->

Commits
-------

09bb1e4 [DoctrineBridge] Fixed submitting invalid ids when using queries with limit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants