I have a field called ‘inclination’ that takes integers from 0 to 90 (degrees).
In the assert statement I state that the type must be integer.
In this case I would like to render the field as a choice-widget, a drop-down list with values ranging from 0 to 90.
I can’t, however, find a way to let the selection happen as integer. Every time I try to submit the form, the error ‘this field must be of type Integer’ shows up.
Is there any way to:
let the combination of a drop-down choice list and an assert\Type(type=”Integer”)
work?
The definition of the property in the entity:
/**
* @ORM\Column(name="inclination", type="integer", nullable=true)
* @Assert\NotBlank()
* @Assert\Min(limit=0)
* @Assert\Max(limit=90)
* @Assert\Type(type="integer")
*/
private $inclination;
The way the field in the form gets built:
$builder->add('inclination', 'choice', array(
'choices' => $this->getInclinationChoices(),
'empty_value' => 'Choose an option',
));
private function getInclinationChoices()
{
$choices = array();
for($i=0; $i<= 90; $i++) {
$choices[$i] = "$i °";
}
return $choices;
}
Not sure if its the proper Symfony 2 approach, but could you force the value to a PHP integer type on the Entity’s
setInclination()method?Eg:
I recreated your issue locally and this fix worked for me.