Symfony如何禁用默认选项 [英] Symfony how to disable the default option
问题描述
根据此网址将默认文本设置为选择(下拉框)/菜单我需要在symfony中为占位符添加 disabled
属性。这是我的代码。
$ builder
- > add('name','text',array(
'标签'=>'商业名称',
))
- > add('country','entity',array(
'class'=>'AppBundle:Country' ,
'property'=>'name',
'placeholder'=>'请选择',
));
现在我需要添加 disabled
code>< option value =disabled>请选择< / option>
我该怎么做?什么是最好的方式来做到这一点?
如果这个话题仍然是开放的...我今天有类似的问题,这样管理它。 (在symfony 2.7.10中测试过)
显然,占位符
是变量中的一个独立属性,因此您不能选择它在 finishView
中。
在 buildForm
方法:
$ b $ pre $ $ builder-> add('label','choice',array(
'data'= > $ var,//您的默认数据
'read_only'=> true,
'placeholder'=> false,//执行条件以检查您想要的选项是否只读
));
实现 finishView
方法 AbstractType
):
public function finishView(FormView $ view,FormInterface $ $ options
foreach($ view-> children ['country'] - > vars ['choices'] as $ id => $ choiceView){$ b $如果($ id!= $ yourAcceptedValueId){
$ choiceView-> attr = ['disabled'=>}; //实现一个条件来阻止接受/希望的选项被禁用。 禁用];
}
}
}
不幸的是,没有占位符选项,所以你必须实现两个设置来实现这一点。
问候
According to this URL Set Default Text in a Select (drop-down) box/menu I need add disabled
attribute for the placeholder in symfony. This is my code.
$builder
->add('name', 'text', array(
'label' => 'Business Name',
))
->add('country', 'entity', array(
'class' => 'AppBundle:Country',
'property' => 'name',
'placeholder' => 'Please select',
));
Now I need to add disabled
like <option value="" disabled>Please select</option>
How can I do it? What is the best way to do that?
if this topic is still open... I had a similar problem today and managed it this way. (tested in symfony 2.7.10)
Obviously the placeholder
is a standalone attribute in vars, therefore you cannot select it in the finishView
.
In your buildForm
method:
$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
));
Implement the finishView
Method (Interface implementation from 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'];
}
}
}
Unfortunately in the choices list is no placeholder option, so you have to implement two settings to achieve this.
Greets
这篇关于Symfony如何禁用默认选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!