Skip to content

[Validator] Add the divisibleBy option to the Count constraint #35783

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

Merged
merged 1 commit into from
Feb 21, 2020

Conversation

fancyweb
Copy link
Contributor

Q A
Branch? master
Bug fix? no
New feature? yes
Deprecations? no
Tickets -
License MIT
Doc PR TODO

From my experience, it is sometimes useful to assert that the number of elements in a collection is a multiple of X.

@nicolas-grekas nicolas-grekas added this to the next milestone Feb 19, 2020
@fancyweb fancyweb force-pushed the validator-count-divisible-by branch from 785c4e0 to 8dfb7b2 Compare February 19, 2020 16:59
@HeahDude
Copy link
Contributor

What issue does this fix considering we already have a dedicated contraint?

@fancyweb
Copy link
Contributor Author

Currently you have to do everything manually:

new Callback(static function ($collection, ExecutionContextInterface $context): void {
    $context
        ->getValidator()
        ->inContext($context)
        ->validate(\count($collection), new DivisibleBy([
            'value' => 3,
        ]));
}),

@HeahDude
Copy link
Contributor

@fancyweb
Copy link
Contributor Author

fancyweb commented Feb 19, 2020

I thought about this but Count is made for collections. Otherwise, we should also support countable for other comparisons constraints such as GreaterThan, LessThan, etc.

@HeahDude
Copy link
Contributor

Why not? :)

@fancyweb
Copy link
Contributor Author

fancyweb commented Feb 19, 2020 via email

@HeahDude
Copy link
Contributor

The collection « count » is divisible by X, not the collection itself.

It is usually different, but when an object is countable, this is just the same thing.

@nicolas-grekas
Copy link
Member

I think this approach (this PR) is more clear to me also.

@fabpot
Copy link
Member

fabpot commented Feb 21, 2020

Thank you @fancyweb.

@fabpot fabpot merged commit f01bbc7 into symfony:master Feb 21, 2020
@fancyweb fancyweb deleted the validator-count-divisible-by branch February 21, 2020 07:54
@nicolas-grekas nicolas-grekas modified the milestones: next, 5.1 May 4, 2020
@fabpot fabpot mentioned this pull request May 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants