【问题标题】:Disabling some checkboxes of choice widget in buildForm()在 buildForm() 中禁用一些选择小部件的复选框
【发布时间】:2013-08-18 13:33:09
【问题描述】:

我有一个“选择”类型的表单小部件,它显示为许多复选框的列表。一切都很好。所以强调一下:有一个小部件,带有许多复选框(而不是几个复选框小部件)。

现在,我想禁用其中一些复选框。该数据在 $options-Array 中可用。

这是我的 FooType.php 的 buildForm() 函数

...
public function buildForm(FormBuilderInterface $builder, array $options)
{
  $builder
    ->add('foo', 'choice', array('choices'  => $options['choiceArray']['id'],
      'multiple' => true,
      'expanded' => true,
      'disabled' => $options['choiceArray']['disabled'] // does not work (needs a boolean)
      'data'     => $options['choiceArray']['checked'], // works
      'attr'     => array('class' => 'checkbox')))
  ;
}
...

我的 Twig 模板如下所示:

{% for foo in fooForm %}

    <dd>{{ form_widget(foo) }}</dd>

{% endfor %}

我只能禁用所有复选框(通过在 buildForm 中设置 'disabled' => true)。并且在那里传递一个数组不起作用(如 sn-p 中所述)。

如何禁用我的选择小部件的某些选中的复选框(存储在 $options['choiceArray']['disabled'] 中)?

【问题讨论】:

  • Symfony EventSubscriber 可以帮助你stackoverflow.com/questions/12642473/…
  • @Milos:感谢您的有益评论!使用 EventSubscriber 似乎是解决方案,但是......实现起来非常复杂。由于我是 Symfony2 新手,我非常感谢一些帮助。在此先感谢您提供任何进一步的提示和 sn-ps! Symfony2 Dynamic Forms
  • 仅当您想根据对象中的当前值确定要禁用哪些选项时,EventSubscriber 才相关。它不会帮助您设置单个选项元素的属性。这是一个常见的要求,没有简单的解决方案。您需要创建表单选择对象或提供您自己的树枝模板。这两种解决方案都不容易,我也不知道有什么好的例子。 JavaScript 可能是你最好的选择。
  • @Cerad:谢谢!并且:是的,我采用了 JavaScript 方式......并详细发布了我的解决方案。
  • This related thread 实际上准确地显示了我的问题并解决了它......但是:它不再适用于 Symfony 2.3!

标签: symfony symfony-forms symfony-2.3


【解决方案1】:

我已经用 JQuery 解决了这个问题。

  • 在我的 FooType.php 中,我将应禁用的字段数组字符串化。
  • 我通过隐藏字段将 buildForm()-Function 中的字符串传递给模板
  • 我使用 JQuery 将字符串再次拆分为 ID 并处理禁用复选框并将标签变灰

这是 PHP 代码(FooType.php):

...
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $disabledCount  = sizeof($options['choiceArray']['disabled']);
    $disabledString = '';

    for ($i = 0; $i < $disabledCount; $i++)
    {
        $disabledString .= $options['choiceArray']['disabled'][$i];

        if ($i < $disabledCount-1)
        {
            $disabledString .= '|';
        }
    }



    $builder
        ->add('foo', 'choice', array('choices'  => $options['choiceArray']['id'],
                                               'multiple' => true,
                                               'expanded' => true,
                                               'data'     => $options['choiceArray']['checked'],
                                               'attr'     => array('class' => 'checkbox')))
        ->add('foo_disabled', 'hidden', array('data' => $disabledString))
    ;
}
...

这里是 JavaScript 部分(Twig-template):

function disableModule()
{
    var disabledString = $('#foo_disabled').val();

    var disabledArray = disabledString.split('|');

    $.each( disabledArray, function( disKey, disVal )
    {
        // deactivate checkboxes
        $('input[id="'+idCurrent+'"]').attr("disabled", true);

        // grey out label for checkboxes
        $('label[for="'+idCurrent+'"]').attr("style", "color: gray;");
    });
}

在我的 Entity/Foo.php 中,我必须使用 setter 和 getter 方法添加字符串类型的属性“foo_disabled”。

【讨论】:

    【解决方案2】:

    此页面首先出现在 Goole 搜索结果中“twig checkbox check and disabled”的搜索结果中 要在 twig 中设置检查或/和禁用某些复选框输入,可以使用choice_attr

    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('email', EmailType::class, ['label'=>'Login: '])
            ->add('roles', ChoiceType::class, [
                'label'=>'Role: ',
                'multiple'=>true,
                'expanded'=>true,
                'choices'=>['User'=>'ROLE_USER','Admin'=>'ROLE_ADMIN'],
                'choice_attr'=> [
                    'User' => ['disabled'=>'disabled', 'checked'=>'checked'],
                    ]
                ])
            ->add('password', PasswordType::class, ['label'=>'Password: '])
            ->add('register', SubmitType::class, ['label' => 'Register'])
        ;
    }
    

    在此示例中,我为 ROLE_USER 设置了选中和禁用复选框,因为这是默认角色。

    【讨论】:

      猜你喜欢
      • 2020-09-24
      • 1970-01-01
      • 1970-01-01
      • 2020-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-16
      • 2012-04-25
      相关资源
      最近更新 更多