Reputation: 2652
I've been trying to add HTML attributes to my radio buttons, but somehow it doesn't work. I'm using the built-in choice_attr
option. In this case I do not want to use a callable, as the definition of choice_attr
says that the value can be a callable, string or array. I'd like to use an array. Below is the code, what am I doing wrong?
->add('plan', ChoiceType::class, array(
'choices' => array(
'Basic' => 0,
'Superb (+ €9)' => 1,
'Super (+ €18)' => 2,
'Rrah' => 3
),
'expanded' => true,
'data' => 0,
'choice_attr' => [
1 => ['data-price' => '0'],
2 => ['data-price' => '9'],
3 => ['data-price' => '18'],
4 => ['data-price' => '0']
])
)
Upvotes: 1
Views: 1008
Reputation: 7764
choice_attr is used for HTML attributes bluppfisk. See the documentation:
http://symfony.com/doc/current/reference/forms/types/choice.html#choice-attr
The parameters of the choice_attr function needs to be one of $val, $key, $index, which represents the value, key and index of the 'choices' array that you use. So you could try:
'choice_attr' => function($val) {
$price = 0;
if ($val == 1) {
$price = 9;
}
elseif ($val == 2) {
$price = 18;
}
return ['data-price' => $price];
}
Upvotes: 2