Reputation: 12740
I have a percentage field in my form and I want it have values between 0 - 100 as number. How can I force it and return validation otherwise?
Currently I can even enter 100.10 which is wrong.
Note: 0.00 and 100.00 is allowed
Thanks
ENTITY:
/**
* @var decimal
* @ORM\Column(type="decimal", precision=5, scale=2)
*/
protected $vatRate;
FORM TYPE:
->add(
'vatRate',
'percent',
array('type' => 'integer', 'attr' => array('min' => '0', 'max' =>` '100'))
)
Upvotes: 0
Views: 2482
Reputation: 105908
Add a constraint to the entity
use Symfony\Component\Validator\Constraints as Assert;
/* ... */
/**
* @var decimal
* @ORM\Column(type="decimal", precision=5, scale=2)
* @Assert\Range(
* min = 0,
* max = 100,
* minMessage = "Min % is 0",
* maxMessage = "Max % is 100"
* )
*/
protected $vatRate;
Upvotes: 3
Reputation: 4304
Use Range constraint in your entity:
http://symfony.com/doc/current/reference/constraints/Range.html
use Symfony\Component\Validator\Constraints as Assert;
/**
* @var decimal
* @ORM\Column(type="decimal", precision=5, scale=2)
* @Assert\Range(
* min = 0,
* max = 100
* )
*/
protected $vatRate;
Upvotes: 1