Min
Edit this pageWarning: You are browsing the documentation for Symfony 2.0, which is no longer maintained.
Consider upgrading your projects to Symfony 7.0.
Min
Validates that a given number is greater than some minimum number.
Applies to | property or method |
Options | |
Class | Min |
Validator | MinValidator |
Basic Usage
To verify that the "age" field of a class is "18" or greater, you might add the following:
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. }
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;
}
1 2 3 4 5 6 7 8 9
<!-- src/Acme/EventBundle/Resources/config/validation.yml -->
<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>
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).