This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony
Fabien Potencier b4f03d0c3b feature #35744 [Validator] Add AtLeastOne constraint and validator (przemyslaw-bogusz)
This PR was squashed before being merged into the 5.1-dev branch.

Discussion
----------

[Validator] Add AtLeastOne constraint and validator

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

This constraint allows you to apply a collection of constraints to a value, and it will be considered valid, if it satisfies at least one of the constraints from the collection.

Some examples:

```php
    /**
     * @Assert\AtLeastOne({
     *     @Assert\Length(min=5),
     *     @Assert\EqualTo("bar")
     * })
     */
    public $name = 'foo';

    /**
     * @Assert\AtLeastOne({
     *     @Assert\All({@Assert\GreaterThanOrEqual(10)}),
     *     @Assert\Count(20)
     * })
     */
    public $numbers = ['3', '5'];

    /**
     * @Assert\All({
     *     @Assert\AtLeastOne({
     *          @Assert\GreaterThanOrEqual(5),
     *          @Assert\LessThanOrEqual(3)
     *     })
     * })
     */
    public $otherNumbers = ['4', '5'];
```

The respective default messages would be:
`name: This value should satisfy at least one of the following constraints: [1] This value is too short. It should have 5 characters or more. [2] This value should be equal to "bar".`

`numbers: This value should satisfy at least one of the following constraints: [1] Each element of this collection should satisfy its own set of constraints. [2] This collection should contain exactly 20 elements.`

`otherNumbers[0]: This value should satisfy at least one of the following constraints: [1] This value should be greater than or equal to 5. [2] This value should be less than or equal to 3.`

But of course you could also create a simple custom message like `None of the constraints are satisfied`.

Commits
-------

e6209a697c [Validator] Add AtLeastOne constraint and validator
2020-03-16 07:29:24 +01:00
..
Bridge feature #36034 [PhpUnitBridge] Deprecate @expectedDeprecation annotation (hkdobrev) 2020-03-16 07:05:20 +01:00
Bundle feature #36003 [ErrorHandler][FrameworkBundle] better error messages in failing tests (guillbdx) 2020-03-16 07:11:56 +01:00
Component feature #35744 [Validator] Add AtLeastOne constraint and validator (przemyslaw-bogusz) 2020-03-16 07:29:24 +01:00
Contracts Merge branch '5.0' 2020-03-15 15:51:35 +01:00