【问题标题】:Symfony how to disable the default optionSymfony 如何禁用默认选项
【发布时间】:2015-03-04 12:10:24
【问题描述】:

根据这个 URL 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',
    )); 

现在我需要像<option value="" disabled>Please select</option>一样添加disabled

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

【问题讨论】:

  • 想一想:禁用“请选择”选项意味着该项目(国家/地区 1)将​​被选中。所以它不会是一个占位符。
  • 自 3 个月以来,您的问题解决了吗?我想知道答案...

标签: forms symfony twig


【解决方案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'];
        }
    }
}

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

问候

【讨论】:

    【解决方案2】:

    这样做更容易:

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

    【讨论】:

    • 这仍然适用于 Symfony5。一个提示:对于 MacOS,您还需要添加 select:invalid { color: #9f9f9f; } 以正确设置样式。
    【解决方案3】:

    既然可以为空值设置值,为什么还要添加禁用选项?

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

    【讨论】:

    • empty_value 是如果用户在未选择选项的情况下提交表单时应在服务器上使用的值。这与强制用户在提交表单之前选择一个选项不同,因此默认选项永远无效。
    【解决方案4】:

    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,
        )); 
    

    【讨论】:

    • 占位符值在我的表单视图中正确显示。我只需要禁用它。
    • 那么 dawid 的回答应该是你想要的。
    【解决方案5】:
    /* ... */
    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';
                }
            }
        }
    /* ... */
    

    【讨论】:

    • finishView方法是怎么触发的?这段代码对我不起作用。
    • 你把这个方法放在哪里了?
    • 我假设,你有自己的 FormType,比如 RegistrationType 什么的,你的类型由 Symfony\Component\Form\AbstractType 扩展,并且有 finishView。
    • 测试这个例子只是为了好玩,看起来 ['country']->children 在这个阶段是空的[]
    【解决方案6】:

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

    【讨论】:

      【解决方案7】:

      应该是这样的:

      $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

      【讨论】:

      • 不,我不需要禁用下拉菜单。我需要禁用空选项。
      猜你喜欢
      • 2016-11-06
      • 1970-01-01
      • 1970-01-01
      • 2011-10-13
      • 1970-01-01
      • 1970-01-01
      • 2019-02-09
      • 1970-01-01
      • 2017-10-23
      相关资源
      最近更新 更多