I created a field type subclass of ChoiceType to add option for empty value, so it's automatically added for every field of such type. But the problem is if we select this value, form validation fails This value is not valid. How could we make this form work for such option? I need this value deleting it is not an option.
class ChoiceNullableType extends ChoiceType
{
public function buildView(FormView $view, FormInterface $form, array $options): void
{
parent::buildView($view, $form, $options);
$nullOption = new ChoiceView(null, 'empty', '(Empty)'));
array_unshift($view->vars['choices'], $nullOption);
}
}
I try to use it like this:
$builder->add('managers', ChoiceNullableType::class, [
'placeholder' => 'empty_value.select_all',
'required' => false,
//Not to add empty field for every field in such methods
'choices' => $this->getManagersList(),
'multiple' => true,
]);