Symfony如何禁用默认选项

时间:2015-03-04 12:10:25

标签: forms symfony twig

根据此网址Set Default Text in a Select (drop-down) box/menu,我需要为symfony中的占位符添加disabled属性。这是我的代码。

$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>

我该怎么办?最好的方法是什么?

7 个答案:

答案 0 :(得分:1)

如果这个主题仍然是开放的......我今天遇到了类似的问题并以这种方式进行管理。 (在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方法(来自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'];
        }
    }
}

不幸的是,在选项列表中没有占位符选项,因此您必须实现两个设置才能实现此目的。

迎接

答案 1 :(得分:0)

/* ... */
use Symfony\Component\Form\FormView;
use Symfony\Component\Form\FormInterface;

class YourBusinessFormType extends AbstractType
{
    public function finishView(FormView $view, FormInterface $form, array $options)
    {
        foreach ($view->children['country']->children as $country) {
            if ($country->vars['value'] == "") {
                $country->vars['attr']['disabled'] = 'disabled';
            }
        }
    }
/* ... */

答案 2 :(得分:0)

为什么在为空值设置值时添加禁用选项?

$builder->add('states', 'entity', array(
    'empty_value' => 'Choisissez une option',
));

答案 3 :(得分:0)

正如in the doc所述,将placeholder参数设置为false以禁用/避免默认选项:

$builder
    ->add('name', 'text', array(
        'label' => 'Business Name',
    ))           
    ->add('country', 'entity', array(
        'class'       => 'AppBundle:Country',
        'property'    => 'name',
        'placeholder' => false,
    )); 

<小时/> 在Symfony2.6之前,您可以改为使用empty_value参数:

$builder
    ->add('name', 'text', array(
        'label' => 'Business Name',
    ))           
    ->add('country', 'entity', array(
        'class'       => 'AppBundle:Country',
        'property'    => 'name',
        'empty_value' => false,
    )); 

答案 4 :(得分:0)

这样做更容易:

$builder->add('civility', ChoiceType::class, [
    'choices' => [
        'Civility*' => '',
        'Mrs.' => 1,
        'Mr.' => 2,
    ],
    'required' => 'required',
    'choice_attr' => ['Civility*' => ['disabled'=>'']]
])

因此,当表单加载时,您将获得:<option value disabled selected="selected">Civility*</option>

然后你可以添加一些css option[disabled] {color: #9f9f9f;}

答案 5 :(得分:0)

您可以创建自己的表单类型,并为此表单类型创建模板。例如:您创建名称为CountrySelectorType的表单类型并设置父项EntityTypeChoiceType。接下来,您需要在form_types/fields.html.twig文件夹中创建一个文件夹和文件app/Resources/views(或任何其他名称),并将此路径添加到config.yml文件中,例如:

twig:
  debug: '%kernel.debug%'
  strict_variables: '%kernel.debug%'
  form_themes:
      - 'form/fields.html.twig'

here可以为您的自定义字段类型找到所有归档的布局

接下来,您只需将父窗口小部件代码复制到您的fields.html.twig文件,根据您的类型字段名称更改窗口小部件名称,然后根据需要对其进行自定义。我也遇到了同样的问题,因此我的自定义城市选择器类型(默认选项已禁用)如下所示:

{% block country_selector_widget %}
    {%- if required and placeholder is none and not placeholder_in_choices and not multiple and (attr.size is not defined or attr.size <= 1) -%}
        {% set required = false %}
    {%- endif -%}
    <select {{ block('widget_attributes') }}{% if multiple %} multiple="multiple"{% endif %} class="form-control">
        {%- if placeholder is not none -%}
            <option {% if is_filer_form_field %}disabled{% endif %} value=""{% if required and value is empty %} selected="selected"{% endif %}>
                {{ placeholder != '' ? (translation_domain is same as(false) ? placeholder : placeholder|trans({}, translation_domain)) }}
            </option>
        {%- endif -%}
        {%- if preferred_choices|length > 0 -%}
            {% set options = preferred_choices %}
            {{- block('choice_widget_options') -}}
            {%- if choices|length > 0 and separator is not none -%}
                <option disabled="disabled">{{ separator }}</option>
            {%- endif -%}
        {%- endif -%}
        {%- set options = choices -%}
        {{- block('choice_widget_options') -}}
    </select>
{% endblock %}

答案 6 :(得分:-2)

它应该是这样的:

$builder
    ->add('name', 'text', array(
        'label' => 'Business Name',
    ))           
    ->add('country', 'entity', array(
        'class' => 'AppBundle:Country',
        'property' => 'name',
        'placeholder' => 'Please select',
        'attr' => array('disabled'=>'disabled')
    )); 

以下是此文档:http://symfony.com/doc/current/reference/forms/types/form.html#attr