In Symfony 3, I want the 'placeholder' of a select list menu to appear before any selection is made but to not appear (or not being select-able) among the list of available options for the user.
The function buildForm() of my MyEntityType, extending AbstractType looks like this:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$list = array([array with choices values and descriptions]);
$builder
->add('description',TextType::class, array('label'=>'trans.my_desc'))
->add('list',ChoiceType::class,array(
'label'=>'trans.my_list',
'multiple'=>false,
'choices'=>$list,
'placeholder'=>'trans.do_a_choice',
//'placeholder_in_choices'=>false //this option is not available
)
->add('submit',SubmitType::class,array('label'=>'trans.validate'));
}
When I look at the doc, I see that there is a boolean option placeholder_in_choices (http://symfony.com/doc/current/reference/forms/types/choice.html#field-variables) but this option cannot be set in the [array of options for list] which is under "->add('list',ChoiceType::class,array([array of options for list])" (it throws an error explaining that 'placeholder_in_choices' is not an available option).
Thru my search I found this already existing question close to mine: Symfony how to disable the default option
After reading thru it, I've tried to implement the finishView() function in MyEntityType class:
public function finishView(FormView $view, FormInterface $form, array $options){
var_dump($view->children['list']->vars);
foreach ($view->children['list']->vars['choices'] as $sma) {
if ($sma->value == "") {
$sma->attr['disabled'] = 'disabled';
}
}
}
The problem is that, as it is shown in the var_dump(), $view->children['list']->vars['choices'] does not include the 'placeholder' value or label (trans.do_a_choice), it is not possible hence to attach a 'disabled' attribute to it.
Does anyone has a clue how to have 'trans.do_a_choice' to be displayed as placeholder in the select menu but to not appear (or not being select-able) in the options list?