簡體   English   中英

Symfony2覆蓋Twig中的表單小部件

[英]Symfony2 override a form widget in Twig

我正在嘗試按照以下文檔覆蓋Twig中的表單小部件: http : //symfony.com/doc/current/cookbook/form/form_customization.html#method-1-inside-the-same-template-as-the -形成

但是我有點迷路了。 這是我要細分的選擇字段:

{{ form_widget(edit_form.activities) }}

這是文檔中采用的主要過程:

    {% form_theme edit_form _self %}

    {%- block choice_widget_options -%}
        {% for group_label, choice in options %}
            {%- if choice is iterable -%}
                <optgroup label="{{ choice_translation_domain is same as(false) ? group_label : group_label|trans({}, choice_translation_domain) }}">
                    {% set options = choice %}
                    {{- block('choice_widget_options') -}}
                </optgroup>
            {%- else -%}
                {% set attr = choice.attr %}
                <option value="{{ choice.value }}" {{ block('attributes') }}{% if choice is selectedchoice(value) %} selected="selected"{% endif %}>{{ choice_translation_domain is same as(false) ? choice.label : choice.label|trans({}, choice_translation_domain) }}</option>
            {%- endif -%}
        {% endfor %}
    {%- endblock choice_widget_options -%}

目的是能夠在選擇框中選擇多個字段,並通過我的控制器發布一系列ID。

你們有什么想法嗎?

用Twig這樣做是行不通的。 所以我在FormType方面做到了。

解決方案:使用PRE_SET_DATA。 我用“數據”選項覆蓋了表格。

這是給您的代碼:

class TagType extends AbstractType
{
    /** @var  $em EntityManager */
    private $em;

    /**
     * TagType constructor.
     *
     * @param EntityManager $entityManager
     */
    public function __construct(EntityManager $entityManager)
    {
        $this->em = $entityManager;
    }

    /**
     * @param FormBuilderInterface $builder
     * @param array                $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name', 'text', [
                'label' => 'Tags',
            ])
            ->add('activities', 'entity', [
                'label'    => 'Affecter à des activitées',
                'mapped'   => false,
                'class'    => 'App\TagBundle\Entity\Activity\Activity',
                'required' => false,
                'multiple' => true,
                'attr'     => [
                    'data-js'          => 'chosen',
                    'data-placeholder' => 'Affecter à des activitées',
                ],
            ]);

        $builder->addEventListener(FormEvents::PRE_SET_DATA, array($this, 'onPreSetData'));
    }

    /**
     * @param FormEvent $event
     */
    public function onPreSetData(FormEvent $event)
    {
        $tag = $event->getData();
        $form = $event->getForm();

        /** Doctrine\ORM\PersistentCollection $taggingCollection */
        $taggingCollection = $tag->getTagging();

        if ($taggingCollection == null)
            return; // leave if no tag (probably a new action)

        $activitiesIds = array();
        /** @var \App\TagBundle\Entity\Activity\Tagging $tagging */
        foreach( $taggingCollection as $tagging){
            $activitiesIds[] = $tagging->getResourceId();
        }

        $em = $this->em;
        $activities = new ArrayCollection();
        foreach($activitiesIds as $activityId) {
            $activity = $em->getRepository('AppTagBundle:Activity\Activity')->find($activityId);
            $activities->add($activity);
        }

        $form->add('activities', 'entity', [
            'label'    => 'Editer les activitées affectées',
            'mapped'   => false,
            'class'    => 'App\TagBundle\Entity\Activity\Activity',
            'required' => false,
            'multiple' => true,
            'attr'     => [
                'data-js'          => 'chosen',
                'data-placeholder' => 'Affecter à des activitées',
            ],
            'data' => $activities
        ]);
    }

    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults([
            'data_class' => 'App\TagBundle\Entity\Activity\Tag',
        ]);
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 'App_TagBundle_activity_tag';
    }
}
Firstly check if path to your template is correct:  app/Resources/views/Form/fields.html.twig

Next, in the template where you are using the form, you have to specify your customized template to be used

{% form_theme form 'AppBundle:Form:fields.html.twig' %}

{{ form_widget(edit_form.activities) }}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM