I got incidents which have a reference to a risk.
I am using Symfony 2.6 and Doctrine ORM entities with the following rather symbolic entities:
class Incident
{
private $id;
private $name;
private $date;
/**
* @ORM\ManyToOne(targetEntity="Risk")
* @ORM\JoinColumn(name="risk_id")
*/
private $risk;
}
class Risk
{
private $id;
private $level;
private $name;
}
The risks
-Table has data like:
id level name
1 0 none
2 2 low
3 1 very low
4 5 moderate
5 10 high
For the incidents I got a form-type like this:
class IncidentType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name')
[...]
->add('risk', 'entity', [
'class' => 'Model:Risk',
'property' => 'name',
'empty_data' => null,
'empty_value' => 'not assessed',
'expanded' => true,
'required' => [true|false](see below),
])
[...]
}
So my incidents risk can be 'not assessed'. That is when there is no risk set (=null
). To keep my model clean I would not want to have a row with a level of -1 and a name of 'not assessed'.
My form works as expected, missing one tiny detail: when the risk
is null
, my list of radiobuttons has no value selected. This happens regardless of the value of the 'required'
-option.
For a null-value I get:
( ) not assessed
( ) none
( ) very low
[...]
I would want:
(x) not assessed
( ) none
( ) very low
[...]
I guess that behaviour is just fine for a (not expanded) drop-down field.
Is there a way to have the 'placeholder'
/'empty_value'
-option of my radiobuttons selected when the data-value is null
?