修改 Symfony Forms html 属性/覆盖单个属性 [英] Modifying Symfony Forms html attributes / overriding a single attribute

查看:21
本文介绍了修改 Symfony Forms html 属性/覆盖单个属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想弄清楚如何使用 Symfony2 表单动态修改 html 属性.

I am trying to figure out how to modify html attributes on the fly with Symfony2 forms.

这种情况是大多数时候使用默认占位符,但偶尔,开发人员需要编写自定义消息.

The situation is a case where a default placeholder is used most of the time, but occasionally, the developer needs to write a custom message.

我的表单类型如下所示:

My Form type looks like this:

    <?php

    namespace My\AwesomeBundle\FormType;

    use Symfony\Component\Form\AbstractType;
    use Symfony\Component\Form\FormBuilderInterface;
    use Symfony\Component\OptionsResolver\OptionsResolverInterface;
    use My\AwesomeBundle\Transformer\ProcedureCodeTransformer;

    class ProcedureType extends AbstractType
    {

        private $em;
        private $user;


        public function __construct($em, $securityContext)
        {
            $this->em=$em;
            $this->user=$securityContext->getToken()->getUser();
        }

        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            $transformer = new ProcedureTransformer( $this->em );
            $builder->addModelTransformer( $transformer );
        }

        public function setDefaultOptions(OptionsResolverInterface $resolver)
        {
            $user = $this->user;
            $resolver->setDefaults(
                array(
                    'class'       => 'My\AwesomeBundle\Entity\Procedure',
                    'label'       => 'Procedure',
                    'label_attr'  => array( 'class'=> 'control-label' ),
                    'required'    => false,
                    'empty_value' => '',

                    'attr'        => array(
                        'class'                    => 's2',
                        'data-select2-placeholder' => 'Select Procedure',
                    ),
                )
            );

            $resolver->setOptional( array( 'placeholder' ) );
        }

        public function getParent() {
            return 'hidden';
        }

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

然后,默认渲染具有用于 data-select2-placeholder 元素的选择过程",并使用 javascript 来显示它.然后在更复杂的类型中使用它:

The default render then has "Select Procedure" for the data-select2-placeholder element and javascript is used to display it. This is then used in a more complex type:

    <?php

    namespace My\AwesomeBundle\FormType;

    use Symfony\Component\Form\AbstractType;
    use Symfony\Component\Form\FormBuilderInterface;
    use Symfony\Component\OptionsResolver\OptionsResolverInterface;

    class ProcedureType extends AbstractType
    {
        public function buildForm(FormBuilderInterface $builder, array $options){
            $builder->add('biller', 'billers', array( 'placeholder' => 'New Text'));

            [...]

        }

        public function setDefaultOptions(OptionsResolverInterface $resolver){
            $resolver->setDefaults( array(
                    'data_class' => 'My\AwesomeBundle\Entity\Procedure'
                ) );
        }

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

我希望将新文本"放入 data-select2-placeholder html 属性中.但是,如果我这样调用构建器:

I would like 'New Text' to be placed into the data-select2-placeholder html attribute. However, if I call the builder like this:

    $builder->add('procedure',
        new ProcedureCodeType()),
        array( 'attr' => array('data-select2-placeholder' => 'New Text') ) 
    );

整个 html 属性数组被替换.这并不奇怪.表单构建器中是否有一个函数让我无法添加或修改单个 html 属性?

The entire html attribute array is replaced. this is not surprising. Is there a function in the form builder that has eluded me to add or modify a single html attribute?

推荐答案

不幸的是,没有办法按照您希望的方式修改单个属性.这是我能想到的最接近和最简单的解决方案:

Unfortunately, there is no way to modify a single attr the way you would want it. This is the closest and most simple solution I could come with:

在你的情况下你必须做的是在你的选项中使用回调函数.

What you have to do in your situation is to use callback functions in your options.

对于表单类型中的每个默认属性,您可以使用回调函数来代替普通值,该回调函数接受表示您的选项的单个输入(Symfony 类 Symfony\Component\OptionsResolver\Options).在构建表单时调用该函数,然后将返回值用作选项的值.

For each default attribute in your form types, instead of a normal value, you can use a callback function that accepts a single input representing your options (Symfony class Symfony\Component\OptionsResolver\Options). The function is called when building the form and the return value is then used as the value of the option.

这样,您可以根据其他提供的选项构建结果选项.一个例子:

That way, you can build the resulting option depending on the other provided options. An example:

use Symfony\Component\OptionsResolver\Options;

class ProcedureType extends AbstractType
{
    ...

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $attr = function(Options $options) {
            $default = array(
                'class'                    => 's2',
                'data-select2-placeholder' => 'Select Procedure',
            );
            return array_replace($default, $options['new_attr']);
        };

        $user = $this->user;
        $resolver->setDefaults(
            array(
                'class'       => 'My\AwesomeBundle\Entity\Procedure',
                'label'       => 'Procedure',
                'label_attr'  => array( 'class'=> 'control-label' ),
                'required'    => false,
                'empty_value' => '',
                'attr'        => $attr,
                'new_attr'    => array(),
            )
        );

        $resolver->setOptional( array( 'placeholder' ) );
    }

这样,您必须修改的是new_attr.

That way, what you have to modify will be new_attr.

这篇关于修改 Symfony Forms html 属性/覆盖单个属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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