Css 将类添加到symfony复选框的子集

Css 将类添加到symfony复选框的子集,css,forms,symfony,Css,Forms,Symfony,我在Symfony中创建了一个不绑定到实体的表单。表单元素之一是一组复选框、字段类型。显示哪些fieldtype复选框应取决于searchby字段的值。我想在每个复选框中添加一个类,用作稍后添加的javascript的显示/隐藏挂钩。但就我所见,form元素只允许应用整个类 以下是我表格中的相关部分: $form = $this->createFormBuilder() ->add('searchby', 'choice', array( 'label

我在Symfony中创建了一个不绑定到实体的表单。表单元素之一是一组复选框、字段类型。显示哪些fieldtype复选框应取决于searchby字段的值。我想在每个复选框中添加一个类,用作稍后添加的javascript的显示/隐藏挂钩。但就我所见,form元素只允许应用整个类

以下是我表格中的相关部分:

$form = $this->createFormBuilder()
    ->add('searchby', 'choice', array(
            'label' => 'browse.label.searchby',
            'choices'=>array(
                'name'              => 'browse.searchby.name',
                'location'          => 'browse.searchby.location',
                'classification'    => 'browse.searchby.classification'
                    ),
            'required' => true,
            'multiple' => false,
            'expanded' => true,
                    ))
    ->add('fieldtypes', 'choice', array(
            'label' => 'browse.label.fieldtypes',
            'choices' => array(
                'extensionAttribute12'      =>  'person.label.position.fr',
                'title'                     =>  'person.label.position.eng',
                'l'                         =>  'person.label.city',
                'st'                        =>  'person.label.province',
                'co'                        =>  'person.label.country',
                'givenname'                 =>  'person.label.firstname',
                'sn'                        =>  'person.label.lastname',
                'name'                      =>  'person.label.fullname',
                ),
            'required' => true,
            'multiple' => true,
            'expanded' => true
                    ));
如果我想将“searchbyname”类添加到由以下内容创建的单选按钮中:
$options['choices']['givenname']、$options['choices']['sn']、$options['choices']['name']
,我该如何操作?

之后,可以这样声明选项:

'choices' => array(
        'classification'    => array(
            'extensionAttribute12'      =>  'person.label.position.fr',
            'title'                     =>  'person.label.position.eng',
            ),
        'location'  => array(
            'l'                         =>  'person.label.city',
            'st'                        =>  'person.label.province',
            'co'                        =>  'person.label.country',
        ),
        'name'  =>  array(
            'givenname'                 =>  'person.label.firstname',
            'sn'                        =>  'person.label.lastname',
            'name'                      =>  'person.label.fullname',
        )
    ),
其中每个子数组的键将用作
中的
标签;在尝试修改Twig模板(正如@Cerad所说的那样,这是一件非常痛苦的事情)之后,我尝试通过创建一个

我的解决方案效率低下,因为我在创建子视图后修改它们,而且我必须包含
ChoiceType::finishView
中的所有代码。我看不出子视图是如何创建的。
ChoiceType::buildForm
中有一行内容是
$remainingViews=$options['choice\u list']->getRemainingViews()
,但由于
$options['choices']
是作为数组输入的,我不知道从哪个类调用
getRemainingViews()

无论如何,这就是:

<?php
namespace Expertise\DefaultBundle\Form\Extension;

use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\FormView;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;


class ChoiceTypeExtension extends AbstractTypeExtension
{
    /**
     * @return string The name of the type being extended
     */
    public function getExtendedType()
    {
        return 'choice';
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setOptional(array('optgroup_as_class'));
        $resolver->setDefaults(array('optgroup_as_class'=>false));
    }


    public function finishView(FormView $view, FormInterface $form, array $options)
    {

        if ($options['expanded']) {
            // Radio buttons should have the same name as the parent
            $childName = $view->vars['full_name'];

            // Checkboxes should append "[]" to allow multiple selection
            if ($options['multiple']) {
                $childName .= '[]';
            }

            foreach ($view as $childView) {
                $childView->vars['full_name'] = $childName;
                if($options['optgroup_as_class']){
                    foreach($options['choices'] as $optclass => $choices){
                        if(!is_array($choices)) continue;
                        foreach($choices as $value => $label){
                            if($childView->vars['value'] == $value && $childView->vars['label'] == $label) {
                                $childView->vars['attr']['class'] =  $optclass;
                                break 2;
                            }
                        }
                    }
                }
            }
        }
    }
}

很遗憾,您无法在表单生成器的选项上设置单个属性。你能调整你的javascript来输入选项值吗?这将是最简单的方法。否则,您将需要自定义表单主题,这将非常痛苦。@Cerad:Drat!我可以根据值来编写javascript,但它没有那么干净,因为添加的选项将意味着在两个位置而不是一个位置进行编辑。我看到了这个答案()。optgroups不用于复选框,但它们应该可用于表单主题,不是吗?我会看看主题,看看变化会有多可怕。