You are browsing the documentation for Symfony 2.2 which is not maintained anymore.
Consider upgrading your projects to Symfony 5.2.
Min
Min¶
Caution
The Min constraint is deprecated since version 2.1 and will be removed
in Symfony 2.3. Use Range with the min
option instead.
Validates that a given number is greater than some minimum number.
Applies to | property or method |
Options | |
Class | Symfony\Component\Validator\Constraints\Min |
Validator | Symfony\Component\Validator\Constraints\MinValidator |
Basic Usage¶
To verify that the “age” field of a class is “18” or greater, you might add the following:
- YAML
1 2 3 4 5
# src/Acme/EventBundle/Resources/config/validation.yml Acme\EventBundle\Entity\Participant: properties: age: - Min: { limit: 18, message: You must be 18 or older to enter. }
- Annotations
1 2 3 4 5 6 7 8 9 10 11 12
// src/Acme/EventBundle/Entity/Participant.php namespace Acme\EventBundle\Entity; use Symfony\Component\Validator\Constraints as Assert; class Participant { /** * @Assert\Min(limit = "18", message = "You must be 18 or older to enter.") */ protected $age; }
- XML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
<!-- src/Acme/EventBundle/Resources/config/validation.yml --> <?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 http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd"> <class name="Acme\EventBundle\Entity\Participant"> <property name="age"> <constraint name="Min"> <option name="limit">18</option> <option name="message">You must be 18 or older to enter.</option> </constraint> </property> </class> </constraint-mapping>
- PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// src/Acme/EventBundle/Entity/Participant.php namespace Acme\EventBundle\Entity\Participant; use Symfony\Component\Validator\Mapping\ClassMetadata; use Symfony\Component\Validator\Constraints as Assert; class Participant { public static function loadValidatorMetadata(ClassMetadata $metadata) { $metadata->addPropertyConstraint('age', new Assert\Min(array( 'limit' => '18', 'message' => 'You must be 18 or older to enter.', ))); } }
Options¶
limit¶
type: integer
[default option]
This required option is the “min” value. Validation will fail if the given value is less than this min value.
message¶
type: string
default: This value should be {{ limit }} or more.
The message that will be shown if the underlying value is less than the limit option.
invalidMessage¶
type: string
default: This value should be a valid number.
The message that will be shown if the underlying value is not a number (per
the is_numeric
PHP function).
This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.