-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
[Validator] Add the When constraint and validator docs #15722
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
Conversation
537e302
to
62eb70b
Compare
62eb70b
to
e539547
Compare
reference/constraints/When.rst
Outdated
* @Assert\GreaterThan(0) | ||
* @Assert\When( | ||
* expression="this.type == 'percent'", | ||
* constraints={@LessThan(100, message="The value should be between 0 and 100!")} |
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.
what about juste writing The value should be less than 100
as it is the @Assert\GreaterThan(0)
that validates the 0
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.
It's just an example and in my opinion, this message is more user friendly.
…uchen90) This PR was merged into the 6.2 branch. Discussion ---------- [Validator] Add the `When` constraint and validator | Q | A | ------------- | --- | Branch? | 6.2 | Bug fix? | no | New feature? | yes <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | License | MIT | Doc PR | symfony/symfony-docs#15722 This constraint allows you to apply constraints validation only if the provided condition is matched. Usage: ```php namespace App\Model; use Symfony\Component\Validator\Constraints as Assert; class Discount { private $type; // 'percent' or 'absolute' /** * `@Assert`\GreaterThan(0) * `@Assert`\When( * expression="this.type == 'percent'", * constraints={`@LessThan`(100, message="The value should be between 0 and 100!")} * ) */ private $value; // ... } ``` See the documentation for details. Commits ------- 9b7bdc9 [Validator] Add the When constraint and validator
…uchen90) This PR was merged into the 6.2 branch. Discussion ---------- [Validator] Add the `When` constraint and validator | Q | A | ------------- | --- | Branch? | 6.2 | Bug fix? | no | New feature? | yes <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | License | MIT | Doc PR | symfony/symfony-docs#15722 This constraint allows you to apply constraints validation only if the provided condition is matched. Usage: ```php namespace App\Model; use Symfony\Component\Validator\Constraints as Assert; class Discount { private $type; // 'percent' or 'absolute' /** * `@Assert`\GreaterThan(0) * `@Assert`\When( * expression="this.type == 'percent'", * constraints={`@LessThan`(100, message="The value should be between 0 and 100!")} * ) */ private $value; // ... } ``` See the documentation for details. Commits ------- 9b7bdc9b18 [Validator] Add the When constraint and validator
…uchen90) This PR was merged into the 6.2 branch. Discussion ---------- [Validator] Add the `When` constraint and validator | Q | A | ------------- | --- | Branch? | 6.2 | Bug fix? | no | New feature? | yes <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | License | MIT | Doc PR | symfony/symfony-docs#15722 This constraint allows you to apply constraints validation only if the provided condition is matched. Usage: ```php namespace App\Model; use Symfony\Component\Validator\Constraints as Assert; class Discount { private $type; // 'percent' or 'absolute' /** * `@Assert`\GreaterThan(0) * `@Assert`\When( * expression="this.type == 'percent'", * constraints={`@LessThan`(100, message="The value should be between 0 and 100!")} * ) */ private $value; // ... } ``` See the documentation for details. Commits ------- 9b7bdc9b18 [Validator] Add the When constraint and validator
e539547
to
c71a1d7
Compare
c71a1d7
to
e842a59
Compare
e842a59
to
a2a82fc
Compare
This is a cool feature! Thank you @wuchen90 |
…uchen90) This PR was merged into the 6.2 branch. Discussion ---------- [Validator] Add the `When` constraint and validator | Q | A | ------------- | --- | Branch? | 6.2 | Bug fix? | no | New feature? | yes <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | License | MIT | Doc PR | symfony/symfony-docs#15722 This constraint allows you to apply constraints validation only if the provided condition is matched. Usage: ```php namespace App\Model; use Symfony\Component\Validator\Constraints as Assert; class Discount { private $type; // 'percent' or 'absolute' /** * `@Assert`\GreaterThan(0) * `@Assert`\When( * expression="this.type == 'percent'", * constraints={`@LessThan`(100, message="The value should be between 0 and 100!")} * ) */ private $value; // ... } ``` See the documentation for details. Commits ------- 9b7bdc9b18 [Validator] Add the When constraint and validator
No description provided.