Symfony twig how to add class to a form row

前端 未结 4 1526
情歌与酒
情歌与酒 2020-12-28 16:58

I am building a project in Symfony 2.3 using Twig. I want to add a class to the form row block. I am using a form theme file which contains:

{% block form_ro         


        
4条回答
  •  长发绾君心
    2020-12-28 17:44

    There is a fairly simple solution to this problem actually. I just needed a form type extension to extend the base form type to allow an extra available option: http://symfony.com/doc/2.3/cookbook/form/create_form_type_extension.html

    Following through the example in the docs, I created a new form type extension:

    // src/Acme/FrontendBundle/Form/Extension/FormTypeExtension.php
    
    namespace Acme\FrontendBundle\Form\Extension;
    
    use Symfony\Component\Form\AbstractTypeExtension;
    use Symfony\Component\Form\FormInterface;
    use Symfony\Component\Form\FormView;
    use Symfony\Component\OptionsResolver\OptionsResolverInterface;
    
    /**
     * Class FormTypeExtension
     * @package Acme\FrontendBundle\Form\Extension
     */
    class FormTypeExtension extends AbstractTypeExtension
    {
        /**
         * Extends the form type which all other types extend
         *
         * @return string The name of the type being extended
         */
        public function getExtendedType()
        {
            return 'form';
        }
    
        /**
         * Add the extra row_attr option
         *
         * @param OptionsResolverInterface $resolver
         */
        public function setDefaultOptions(OptionsResolverInterface $resolver)
        {
            $resolver->setDefaults(array(
                'row_attr' => array()
            ));
        }
    
        /**
         * Pass the set row_attr options to the view
         *
         * @param FormView $view
         * @param FormInterface $form
         * @param array $options
         */
        public function buildView(FormView $view, FormInterface $form, array $options)
        {
            $view->vars['row_attr'] = $options['row_attr'];
        }
    }
    

    Then I registered the service in my bundle...

    
    
        
    
    

    Since every widget extends the base form type this then allows me to pass this new row_attr option through on any field, eg:

    $builder
        ->add('first_name', 'text', array(
            'row_attr' => array(
                'class' => 'form-row-split'
            )
        ));
    

    Then the twig overrides to make use of the new row_attr option:

    {% block form_row %}
        
    {{ form_label(form) }} {{ form_widget(form) }} {{ form_errors(form) }}
    {% endblock form_row %} {% block form_row_attributes %} {% spaceless %} {% for attrname, attrvalue in row_attr %}{{ attrname }}="{{ attrvalue }}" {% endfor %} {% endspaceless %} {% endblock form_row_attributes %}

    And it's done!

    (For completeness, my full twig override still merges in the form-row and error classes in like so:

    {% set row_attr = row_attr|merge({'class': 'form-row' ~ (row_attr.class is defined ? ' ' ~ row_attr.class : '') ~ (errors|length > 0 ? ' error' : '')} ) %}
    

    .. but thats not really necessary for answering my own question :P )

提交回复
热议问题