Skip to content

[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

Merged

Conversation

colinodell
Copy link
Contributor

@colinodell colinodell commented Jul 26, 2018

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.

@colinodell colinodell force-pushed the feature/multiple-of-validator branch from b96bb3a to f8cf896 Compare July 26, 2018 19:39
@colinodell colinodell changed the title Implement validation constraint for testing whether divisibility [Validator] New MultipleOf constraint for testing divisibility Jul 26, 2018
@colinodell colinodell force-pushed the feature/multiple-of-validator branch from f8cf896 to 36e369e Compare July 26, 2018 19:41
@colinodell
Copy link
Contributor Author

The AppVeyor failure seems to be unrelated to this change.

@javiereguiluz javiereguiluz added this to the next milestone Jul 27, 2018
Copy link
Member

@weaverryan weaverryan left a 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 :).

@colinodell
Copy link
Contributor Author

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 modulus == 0 constraint like this would round out Symfony's other comparison constrains nicely.

@nicolas-grekas
Copy link
Member

nicolas-grekas commented Aug 10, 2018

What about renaming to DivisibleBy? I first read MultipleOf as something like SeveralOf, which made no sense.

@xabbuh
Copy link
Member

xabbuh commented Aug 10, 2018

I agree with Nicolas. And since the test in Twig is named the same that would feel more consistent.

@colinodell
Copy link
Contributor Author

Thanks for the feedback @nicolas-grekas! I have renamed the constraint and updated the docs PR accordingly.

@colinodell colinodell changed the title [Validator] New MultipleOf constraint for testing divisibility [Validator] New DivisibleBy constraint for testing divisibility Aug 10, 2018
self::NOT_DIVISIBLE_BY => 'NOT_MULTIPLE_OF',
);

public $message = 'This value should be divisible by {{ compared_value }}.';
Copy link
Member

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.

Copy link
Contributor

@curry684 curry684 Aug 10, 2018

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.

Copy link
Contributor Author

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.

Copy link
Contributor Author

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',
Copy link
Contributor

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.

Copy link
Contributor Author

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!

@colinodell colinodell force-pushed the feature/multiple-of-validator branch 4 times, most recently from bb9feb7 to 6eff196 Compare August 10, 2018 17:03
@curry684
Copy link
Contributor

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>

@dmaicher
Copy link
Contributor

dmaicher commented Aug 13, 2018

For German (de):

Dieser Wert sollte ein Vielfaches von {{ compared_value }} sein.

@xabbuh
Copy link
Member

xabbuh commented Aug 13, 2018

@dmaicher Though we need to capitalise the V (see https://www.duden.de/rechtschreibung/Vielfaches):

Dieser Wert sollte ein Vielfaches von {{ compared_value }} sein.

@colinodell
Copy link
Contributor Author

I have added the two translations (but now fabbot is failing due to an existing translation)

@curry684
Copy link
Contributor

The correction is wrong. @fabpot you should probably exempt .xlf files from typo checks in fabbot if it only searches for English typos 😉

*/
protected function compareValues($value1, $value2)
{
return 0 == fmod($value1, $value2);
Copy link
Member

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

Copy link
Member

@nicolas-grekas nicolas-grekas left a 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 }}.

@colinodell
Copy link
Contributor Author

Thanks for the feedback @nicolas-grekas! I have made the comparison strict and added the French translation.

@nicolas-grekas nicolas-grekas force-pushed the feature/multiple-of-validator branch from 3993781 to efcfb8b Compare August 15, 2018 15:44
@nicolas-grekas nicolas-grekas merged commit efcfb8b into symfony:master Aug 15, 2018
nicolas-grekas added a commit that referenced this pull request Aug 15, 2018
…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
javiereguiluz added a commit to symfony/symfony-docs that referenced this pull request Sep 6, 2018
…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
@nicolas-grekas nicolas-grekas modified the milestones: next, 4.2 Nov 1, 2018
This was referenced Nov 3, 2018
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.

8 participants