在 buildForm() 中禁用一些选择小部件的复选框 [英] Disabling some checkboxes of choice widget in buildForm()

查看:29
本文介绍了在 buildForm() 中禁用一些选择小部件的复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a form widget of type "choice" which is being displayed as a list of many checkboxes. Everything works great. So to stress it out: there is ONE widget, with MANY checkboxes (and NOT several checkbox widgets).

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

Now, I want to disable some of those checkboxes. The data for that is avaliable in the $options-Array.

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

Here is the buildForm()-function of my FooType.php

...
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 模板如下所示:

My Twig-template looks like this:

{% for foo in fooForm %}

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

{% endfor %}

我只能禁用所有复选框(通过在 buildForm 中设置 'disabled' => true).并在那里传递数组不起作用(如代码段中所述).

I can only disable ALL the checkboxes (by setting 'disabled' => true in buildForm). And passing an array there does not work (as commented in the snippet).

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

How can I disable some selected checkboxed (stored in $options['choiceArray']['disabled']) of my choice widget?

推荐答案

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

I have solved the problem using JQuery.

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

这是 PHP 代码 (FooType.php):

Here is the PHP-Code (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 模板):

Here is the JavaScript part (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".

In my Entity/Foo.php I had to add the property "foo_disabled" of type string with setter and getter methods.

这篇关于在 buildForm() 中禁用一些选择小部件的复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆