-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Validator] New DivisibleBy
constraint for testing divisibility
#28069
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
[Validator] New DivisibleBy
constraint for testing divisibility
#28069
Conversation
b96bb3a
to
f8cf896
Compare
MultipleOf
constraint for testing divisibility
f8cf896
to
36e369e
Compare
The AppVeyor failure seems to be unrelated to this change. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice job Colin! I just tested this in a real project - no missing pieces - it works great :).
Thanks Ryan! I'm currently using this in an application with a "time spent" field which only accepts 0.25 hour increments. I feel like having a |
What about renaming to |
I agree with Nicolas. And since the test in Twig is named the same that would feel more consistent. |
Thanks for the feedback @nicolas-grekas! I have renamed the constraint and updated the docs PR accordingly. |
MultipleOf
constraint for testing divisibilityDivisibleBy
constraint for testing divisibility
self::NOT_DIVISIBLE_BY => 'NOT_MULTIPLE_OF', | ||
); | ||
|
||
public $message = 'This value should be divisible by {{ compared_value }}.'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be nice to also add translations for this string.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The message is also not all that clear from a user's perspective. To a developer "8 is divisible by 2" makes sense, but a mathematician would say "so is 7.6543", and a common user would just be like "erm keep the math out of my way".
I'd stick with the more generic This value must be a multiple of {{ compared_value }}.
for the end-user feedback.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have updated the message as requested and force-pushed the changes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have also added translation strings in English and Spanish.
const NOT_DIVISIBLE_BY = '6d99d6c3-1464-4ccf-bdc7-14d083cf455c'; | ||
|
||
protected static $errorNames = array( | ||
self::NOT_DIVISIBLE_BY => 'NOT_MULTIPLE_OF', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Forgot to update the constant value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed and force-pushed. Thanks for catching this!
bb9feb7
to
6eff196
Compare
For Dutch (nl): <trans-unit id="84">
<source>This value should be a multiple of {{ compared_value }}.</source>
<target>Deze waarde moet een veelvoud zijn van {{ compared_value }}.</target>
</trans-unit> |
For German (de):
|
@dmaicher Though we need to capitalise the
|
I have added the two translations (but now fabbot is failing due to an existing translation) |
The correction is wrong. @fabpot you should probably exempt |
*/ | ||
protected function compareValues($value1, $value2) | ||
{ | ||
return 0 == fmod($value1, $value2); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's use strict comparison here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For french:
Cette valeur doit être un multiple de {{ compared_value }}.
Thanks for the feedback @nicolas-grekas! I have made the comparison strict and added the French translation. |
3993781
to
efcfb8b
Compare
…ivisibility (colinodell) This PR was squashed before being merged into the 4.2-dev branch (closes #28069). Discussion ---------- [Validator] New `DivisibleBy` constraint for testing divisibility This introduces a new ~`MultipleOf`~ `DivisibleBy` constraint which checks whether one number is a multiple of (aka "divisible by") some other number. Useful for enforcing specific increments on a number. | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | n/a | License | MIT | Doc PR | symfony/symfony-docs#10121 See symfony/symfony-docs#10121 for examples of this constraint in action. Commits ------- efcfb8b [Validator] New `DivisibleBy` constraint for testing divisibility
…ell) This PR was merged into the master branch. Discussion ---------- [Validator] Document the DivisibleBy constraint Documentation for the new ~`MultipleOf`~ `DivisibleBy` constraint proposed in symfony/symfony#28069 Commits ------- 3270cca Document the DivisibleBy constraint
This introduces a new
MultipleOf
DivisibleBy
constraint which checks whether one number is a multiple of (aka "divisible by") some other number. Useful for enforcing specific increments on a number.See symfony/symfony-docs#10121 for examples of this constraint in action.