这并不容易,但您可以使用自定义表单类型字段和 Twig 主题:
1.创建一个新的字段类型,如ChoiceInputType。该类应包含两种字段类型,ChoiceType 和 TextType。
/**
* Class ChoiceInputType
*/
class ChoiceInputType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add(
'choiceType',
ChoiceType::class,
[
'required' => false,
'choices' => $options['choices'],
'label' => 'Make your choice...',
]
)
->add(
'choiceInput',
TextType::class,
[
'required' => false,
'label' => false,
'attr' => [
'placeholder' => 'Other choice...',
],
]
);
}
/**
* {@inheritdoc}
*/
public function buildView(FormView $view, FormInterface $form, array $options): void
{
$view->vars['choices'] = $options['choices'];
}
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setRequired(['choices']);
$resolver->setDefaults(
[
'choices' => [],
]
);
}
/**
* {@inheritdoc}
*/
public function getName(): string
{
return 'choiceInputType';
}
}
2.在您的新自定义字段类型上创建twig theme 并根据需要对其进行组织。
{% block choiceInputType_widget %}
... custom here ...
{% endblock %}
3.在表单中使用新的ChoiceInputType。
$builder->add(
'choiceInputType',
ChoiceInputType::class,
[
'mapped' => false,
'block_prefix' => 'choiceInputType',
'label' => false,
'choices' => [
'test 1' => 1,
'test 2' => 2,
'test 3' => 3,
]
]
);
这里有一些与此主题类似的其他问题的链接,可以帮助您: