Symfony2,如何根据值设置属性类?

时间:2022-10-16 18:25:20

I know the solution with twig, but this is not cool. Maybe someone knows a solution with form type.

我知道枝条的解决方案,但这并不酷。也许有人知道表单类型的解决方案。

I want set class(CSS) for select depending on default value, for example:

我想根据默认值设置类(CSS),例如:

<select>
    <option value="r" selected="selected">read</option>
    <option value="rw">read-write</option>
    <option value="n">no-access</option>
</select>

I want set class to select like:

我希望set class选择如下:

<select class="select-r">
    <option value="r" selected="selected">read</option>
    <option value="rw">read-write</option>
    <option value="n">no-access</option>
</select>

Form type:

class UserDefaultAccessSettingType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $choice = array(User::ACCESS_READ => 'read', User::ACCESS_WRITE => 'read-write', User::ACCESS_DENIED => 'no-access');

        $builder
            ->add('month_report', 'choice', array(
                'choices'   => $choice,
                'required'  => true
            ))
            ->add('quarterly_report', 'choice', array(
                'choices'   => $choice,
                'required'  => true
            ))
            ->add('semi_annual_report', 'choice', array(
                'choices'   => $choice,
                'required'  => true
            ))
            ->add('annual_report', 'choice', array(
                'choices'   => $choice,
                'required'  => true
            ))
        ;
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => '\backend\backendBundle\Entity\UserDefaultAccessSetting',
        ));
    }


    public function getName()
    {
        return 'UserDefaultAccessId';
    }
}

1 个解决方案

#1


Just use the field attr option. According to your code, you assign the choices with your $choice array, so the default choice will be the the first value in the array.
You could do something like this:

只需使用字段attr选项。根据您的代码,您可以使用$ choice数组分配选项,因此默认选项将是数组中的第一个值。你可以这样做:

->add('month_report', 'choice', array(
    'choices'  => $choice,
    'required' => true
    'attr'     => array('class' => 'select-' . array_keys($shoice)[0])
))

If you also want to change the class dynamically depending on the choice the user selects, you need to write some JS for that.

如果您还想根据用户选择的选项动态更改类,则需要为此编写一些JS。

That said, I don't know why you say it "is not cool" to do this in Twig, because as it is style, it probably should be in the view component of your app... In fact, if you're writing JS to handle this anyway, why would you duplicate the presentation logic in the form builder?

也就是说,我不知道为什么你说在Twig中这样做“并不酷”,因为它是风格,它可能应该在你的应用程序的视图组件中......事实上,如果你是无论如何编写JS来处理这个问题,为什么要在表单构建器中复制表示逻辑?

#1


Just use the field attr option. According to your code, you assign the choices with your $choice array, so the default choice will be the the first value in the array.
You could do something like this:

只需使用字段attr选项。根据您的代码,您可以使用$ choice数组分配选项,因此默认选项将是数组中的第一个值。你可以这样做:

->add('month_report', 'choice', array(
    'choices'  => $choice,
    'required' => true
    'attr'     => array('class' => 'select-' . array_keys($shoice)[0])
))

If you also want to change the class dynamically depending on the choice the user selects, you need to write some JS for that.

如果您还想根据用户选择的选项动态更改类,则需要为此编写一些JS。

That said, I don't know why you say it "is not cool" to do this in Twig, because as it is style, it probably should be in the view component of your app... In fact, if you're writing JS to handle this anyway, why would you duplicate the presentation logic in the form builder?

也就是说,我不知道为什么你说在Twig中这样做“并不酷”,因为它是风格,它可能应该在你的应用程序的视图组件中......事实上,如果你是无论如何编写JS来处理这个问题,为什么要在表单构建器中复制表示逻辑?