Symfony2覆盖Twig中的表单小部件

时间:2015-10-29 10:18:51

标签: php forms symfony twig

我尝试按照此文档覆盖Twig中的表单小部件: http://symfony.com/doc/current/cookbook/form/form_customization.html#method-1-inside-the-same-template-as-the-form

但我有点失落。 以下是我想要细分的选择字段:

{{ form_widget(edit_form.activities) }}

以下是doc:

中采用的最重要的流程
    {% 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。

你们有任何想法怎么做?

2 个答案:

答案 0 :(得分:1)

使用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';
    }
}

答案 1 :(得分:-1)

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) }}