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
[Constraints] New Entity Count constraint #12455
Comments
it's very good idea and useful but not really sure if it should be part of Symfony core |
@AhmedSamy as a matter of fact, this constraint is already in the Symfony core ... except that it's a bit different. The UniqueEntityValidator checks in this line if there are more than 1 entity. So we could almost copy-paste that constraint and turn it into |
This is not so simple, because you need to know whether the object you are validating is one of the match found in the DB, or if it is another one, which would add one more entity with this count. |
I think this constraint is applicable to only fresh entities, and I think that's fine, in fact, even better. Given you are PATCH'ing and existing entity, you probably wouldn't want this behavior. |
This looks like an edge case to me that should rather live outside the Validator component. Therefore I am closing here. Thank you for proposing anyway. |
Hi,
I am proposing a new constraint that validates the count of entities cannot exceed specified maximum using specified fields.
Not like existing count constraint which is used on specific field
More like UniqueEntity.
E.g.
So what happens in background, Symfony makes a count query using configured DBAL with the specified fields as discriminators, if the result exceeds max, then a violation is raised.
This would be an extremely helpful constraint used on almost any entity that can be created by user, especially via APIs to limit abuse.
I am aware that this example could be achieved by setting count constraint on the inverse side of relation. However, this is not always the case. You might want to stop the system creating entities that do not have relations when their count exceeds certain amount.
Thanks
The text was updated successfully, but these errors were encountered: