Skip to content

[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

Merged
merged 1 commit into from
Oct 23, 2022

Conversation

wuchen90
Copy link
Contributor

No description provided.

@wuchen90 wuchen90 requested a review from xabbuh as a code owner August 16, 2021 16:42
@wuchen90 wuchen90 force-pushed the conditional-validator branch 2 times, most recently from 537e302 to 62eb70b Compare August 17, 2021 12:20
@wuchen90 wuchen90 changed the base branch from 5.4 to 6.2 August 8, 2022 12:19
@wuchen90 wuchen90 force-pushed the conditional-validator branch from 62eb70b to e539547 Compare August 8, 2022 12:24
@wuchen90 wuchen90 changed the title [Validator] Add the Conditional constraint and validator docs [Validator] Add the When constraint and validator docs Aug 8, 2022
@javiereguiluz javiereguiluz added the Waiting Code Merge Docs for features pending to be merged label Aug 9, 2022
@carsonbot carsonbot added this to the next milestone Aug 9, 2022
* @Assert\GreaterThan(0)
* @Assert\When(
* expression="this.type == 'percent'",
* constraints={@LessThan(100, message="The value should be between 0 and 100!")}
Copy link
Contributor

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

Copy link
Contributor Author

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.

xabbuh added a commit to symfony/symfony that referenced this pull request Sep 11, 2022
…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
@xabbuh xabbuh removed the Waiting Code Merge Docs for features pending to be merged label Sep 11, 2022
@xabbuh xabbuh modified the milestones: next, 6.2 Sep 11, 2022
symfony-splitter pushed a commit to symfony/framework-bundle that referenced this pull request Sep 11, 2022
…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
symfony-splitter pushed a commit to symfony/validator that referenced this pull request Sep 11, 2022
…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
@wuchen90 wuchen90 force-pushed the conditional-validator branch from e539547 to c71a1d7 Compare September 19, 2022 15:11
@wuchen90 wuchen90 force-pushed the conditional-validator branch from c71a1d7 to e842a59 Compare October 6, 2022 08:06
@wouterj wouterj force-pushed the conditional-validator branch from e842a59 to a2a82fc Compare October 23, 2022 13:53
@wouterj
Copy link
Member

wouterj commented Oct 23, 2022

This is a cool feature! Thank you @wuchen90

@wouterj wouterj merged commit 4a0e943 into symfony:6.2 Oct 23, 2022
@wuchen90 wuchen90 deleted the conditional-validator branch October 24, 2022 07:19
symfony-splitter pushed a commit to symfony/framework-bundle that referenced this pull request Jul 28, 2023
…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
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.

6 participants