September 14, 2018 Javier Eguiluz

In Symfony 4.2, the Validator component has introduced a new DivisibleBy constraint to check whether one number is a multiple of ("divisible by") some other number. It's mostly useful for enforcing specific increments on a number:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 // src/Entity/Item.php namespace App\Entity ; use Symfony\Component\Validator\Constraints as Assert ; class Item { /** * @Assert\DivisibleBy(0.25) */ protected $weight ; /** * @Assert\DivisibleBy( * value = 5, * message = "This item requires to be stocked in multiples of 5 units." * ) */ protected $quantity ; }