Symfony News

New in Symfony 4.3: Number constraints

Jan Sch├Ądlich

Contributed by
Jan Sch├Ądlich
in #28637.

The Symfony Validator component was originally based on the Java JSR-303 Bean Validation specification. While reviewing the Bean Validation 2.0 (JSR 380) specification, we found some new constraints that could be useful for Symfony applications.

That's why in Symfony 4.3 we've added four new constraints related to numbers: Positive, PositiveOrZero, Negative and NegativeOrZero. Although you could already validate that a number is positive/negative with the comparison constraints (GreaterThan, LessThanOrEqual, etc.) these new constraints will make your code easier to read and understand:

 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
use Symfony\Component\Validator\Constraints as Assert;

class Person
{
    /** @Assert\PositiveOrZero */
    protected $siblings;

    // ...
}

class Employee
{
    /** @Assert\Positive */
    protected $income;

    // ...
}

class UnderGroundGarage
{
    /** @Assert\NegativeOrZero */
    protected $level;

    // ...
}

class TransferItem
{
    /** @Assert\Negative */
    protected $withdraw;

    // ...
}

Be trained by Symfony experts - 2019-04-29 Clichy - 2019-05-2 Clichy - 2019-05-13 Cologne


About us

What a Symfony developer should know about the framework: News, Jobs, Tweets, Events, Videos,...

Find us on Twitter

Find us on Facebook

ADS