根据这个网址 Set Default Text in a Select (drop-down) box/menu我需要添加 disabled
symfony 中占位符的属性。这是我的代码。
$builder
->add('name', 'text', array(
'label' => 'Business Name',
))
->add('country', 'entity', array(
'class' => 'AppBundle:Country',
'property' => 'name',
'placeholder' => 'Please select',
));
现在我需要添加
disabled
喜欢 <option value="" disabled>Please select</option>
我该怎么做?最好的方法是什么?
最佳答案
如果这个话题仍然开放......我今天遇到了类似的问题并以这种方式管理它。 (在 symfony 2.7.10 中测试)
显然 placeholder
是 vars 中的独立属性,因此您不能在 finishView
中选择它.
在您的 buildForm
方法:
$builder->add('label', 'choice', array(
'data' => $var, // your default data
'read_only' => true,
'placeholder' => false, // implement a condition to check wether you want the choice read only
));
实现
finishView
方法(接口(interface)实现来自AbstractType
):public function finishView(FormView $view, FormInterface $form, array $options)
{
foreach ($view->children['country']->vars['choices'] as $id => $choiceView) {
// implement a condition to prevent the accepted/wanted option be disabled
if ($id != $yourAcceptedValueId) {
$choiceView->attr = ['disabled' => 'disabled'];
}
}
}
不幸的是,选择列表中没有占位符选项,因此您必须实现两个设置来实现这一点。
问候
关于forms - Symfony 如何禁用默认选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28854164/