Skip to content
  • About
    • What is Symfony?
    • Community
    • News
    • Contributing
    • Support
  • Documentation
    • Symfony Docs
    • Symfony Book
    • Screencasts
    • Symfony Bundles
    • Symfony Cloud
    • Training
  • Services
    • Platform.sh for Symfony Best platform to deploy Symfony apps
    • SymfonyInsight Automatic quality checks for your apps
    • Symfony Certification Prove your knowledge and boost your career
    • SensioLabs Professional services to help you with Symfony
    • Blackfire Profile and monitor performance of your apps
  • Other
  • Blog
  • Download
sponsored by
  1. Home
  2. Documentation
  3. Reference
  4. Constraints
  5. AtLeastOneOf

AtLeastOneOf

Edit this page

This constraint checks that the value satisfies at least one of the given constraints. The validation stops as soon as one constraint is satisfied.

Applies to property or method
Class AtLeastOneOf
Validator AtLeastOneOfValidator

Basic Usage

The following constraints ensure that:

  • the password of a Student either contains # or is at least 10 characters long;
  • the grades of a Student is an array which contains at least 3 elements or that each element is greater than or equal to 5.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// src/Entity/Student.php
namespace App\Entity;

use Symfony\Component\Validator\Constraints as Assert;

class Student
{
    #[Assert\AtLeastOneOf([
        new Assert\Regex('/#/'),
        new Assert\Length(min: 10),
    ])]
    protected string $plainPassword;

    #[Assert\AtLeastOneOf([
        new Assert\Count(min: 3),
        new Assert\All(
            new Assert\GreaterThanOrEqual(5)
        ),
    ])]
    protected array $grades;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# config/validator/validation.yaml
App\Entity\Student:
    properties:
        password:
            - AtLeastOneOf:
                - Regex: '/#/'
                - Length:
                    min: 10
        grades:
            - AtLeastOneOf:
                - Count:
                    min: 3
                - All:
                    - GreaterThanOrEqual: 5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<!-- config/validator/validation.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping https://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">

    <class name="App\Entity\Student">
        <property name="password">
            <constraint name="AtLeastOneOf">
                <option name="constraints">
                    <constraint name="Regex">
                        <option name="pattern">/#/</option>
                    </constraint>
                    <constraint name="Length">
                        <option name="min">10</option>
                    </constraint>
                </option>
            </constraint>
        </property>
        <property name="grades">
            <constraint name="AtLeastOneOf">
                <option name="constraints">
                    <constraint name="Count">
                        <option name="min">3</option>
                    </constraint>
                    <constraint name="All">
                        <option name="constraints">
                            <constraint name="GreaterThanOrEqual">
                                5
                            </constraint>
                        </option>
                    </constraint>
                </option>
            </constraint>
        </property>
    </class>
</constraint-mapping>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// src/Entity/Student.php
namespace App\Entity;

use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Mapping\ClassMetadata;

class Student
{
    public static function loadValidatorMetadata(ClassMetadata $metadata): void
    {
        $metadata->addPropertyConstraint('password', new Assert\AtLeastOneOf([
            'constraints' => [
                new Assert\Regex(['pattern' => '/#/']),
                new Assert\Length(['min' => 10]),
            ],
        ]));

        $metadata->addPropertyConstraint('grades', new Assert\AtLeastOneOf([
            'constraints' => [
                new Assert\Count(['min' => 3]),
                new Assert\All([
                    'constraints' => [
                        new Assert\GreaterThanOrEqual(5),
                    ],
                ]),
            ],
        ]));
    }
}

Options

constraints

type: array [default option]

This required option is the array of validation constraints from which at least one of has to be satisfied in order for the validation to succeed.

includeInternalMessages

type: boolean default: true

If set to true, the message that is shown if the validation fails, will include the list of messages for the internal constraints. See option message for an example.

message

type: string default: This value should satisfy at least one of the following constraints:

This is the intro of the message that will be shown if the validation fails. By default, it will be followed by the list of messages for the internal constraints (configurable by includeInternalMessages option) . For example, if the above grades property fails to validate, the message will be This value should satisfy at least one of the following constraints: [1] This collection should contain 3 elements or more. [2] Each element of this collection should satisfy its own set of constraints.

messageCollection

type: string default: Each element of this collection should satisfy its own set of constraints.

This is the message that will be shown if the validation fails and the internal constraint is either All or Collection. See option message for an example.

groups

type: array | string default: null

It defines the validation group or groups of this constraint. Read more about validation groups.

payload

type: mixed default: null

This option can be used to attach arbitrary domain-specific data to a constraint. The configured payload is not used by the Validator component, but its processing is completely up to you.

For example, you may want to use several error levels to present failed constraints differently in the front-end depending on the severity of the error.

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
TOC
    Version

    Symfony 7.1 is backed by

    Symfony Code Performance Profiling

    Symfony Code Performance Profiling

    Save your teams and projects before they sink

    Save your teams and projects before they sink

    Version:

    Table of Contents

    • Basic Usage
    • Options
      • constraints
      • includeInternalMessages
      • message
      • messageCollection
      • groups
      • payload

    Symfony footer

    Avatar of Luis Cordova, a Symfony contributor

    Thanks Luis Cordova (@cordoval) for being a Symfony contributor

    83 commits • 2.11K lines changed

    View all contributors that help us make Symfony

    Become a Symfony contributor

    Be an active part of the community and contribute ideas, code and bug fixes. Both experts and newcomers are welcome.

    Learn how to contribute

    Symfony™ is a trademark of Symfony SAS. All rights reserved.

    • What is Symfony?

      • What is Symfony?
      • Symfony at a Glance
      • Symfony Components
      • Symfony Releases
      • Security Policy
      • Logo & Screenshots
      • Trademark & Licenses
      • symfony1 Legacy
    • Learn Symfony

      • Symfony Docs
      • Symfony Book
      • Reference
      • Bundles
      • Best Practices
      • Training
      • eLearning Platform
      • Certification
    • Screencasts

      • Learn Symfony
      • Learn PHP
      • Learn JavaScript
      • Learn Drupal
      • Learn RESTful APIs
    • Community

      • Symfony Community
      • SymfonyConnect
      • Events & Meetups
      • Projects using Symfony
      • Contributors
      • Symfony Jobs
      • Backers
      • Code of Conduct
      • Downloads Stats
      • Support
    • Blog

      • All Blog Posts
      • A Week of Symfony
      • Case Studies
      • Cloud
      • Community
      • Conferences
      • Diversity
      • Living on the edge
      • Releases
      • Security Advisories
      • Symfony Insight
      • Twig
      • SensioLabs Blog
    • Services

      • SensioLabs services
      • Train developers
      • Manage your project quality
      • Improve your project performance
      • Host Symfony projects

      Powered by

    Follow Symfony