在symfony表单中向choiceType添加注解

cyej8jka  于 2023-02-09  发布在  其他
关注(0)|答案(1)|浏览(138)

我想在Symfony中的ChoiceType字段中添加一个额外的注解。这是我的方法。它工作正常,但我很好奇是否有更好的方法。
在我的控制器中,我有一个注解数组,并将数组传递给我的formType。数组的值也可以包含一些HTML标记。

$comments[0] = 'Lorem ipsum dolor sit amet,consectetur adipiscing elit. Praesent nec pellentesque velit, ut porta erat. Etiam convallis rhoncus erat vitae sodales. Vivamus porta tellus erat, non congue urna blandit eget. Nam urna dolor, lacinia luctus ex sed, ornare luctus eros. Morbi varius est a nisi facilisis, bibendum dictum turpis maximus.';
$comments[1] = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent nec pellentesque velit, ut porta erat. Etiam convallis rhoncus erat vitae sodales. Vivamus porta tellus erat, non congue urna blandit eget. Nam urna dolor, lacinia luctus ex sed, ornare luctus eros. Morbi varius est a nisi facilisis, bibendum dictum turpis maximus.';
$comments[2] = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent nec pellentesque velit, ut porta erat. Etiam convallis rhoncus erat vitae sodales. Vivamus porta tellus erat, non congue urna blandit eget. Nam urna dolor, lacinia luctus ex sed, ornare luctus eros. Morbi varius est a nisi facilisis, bibendum dictum turpis maximus.';

$form = $this->createForm(UserSessionType::class, $userSession, ['choices' => $choices, 'comments' => $comments, 'amounts' => $amounts]);

我的formType如下所示。我捕获注解并将其放入数据属性中

$this->comments = $options['comments'];

 $builder
                ->add('funding', ChoiceType::class, [
                    'mapped' =>false,
                    'choices' => $this->choices,
                    'choice_attr' => function($choiceValue, $key, $index) {
                        return ['data-comment' => $this->comments[$index]];
                    },
                    'expanded' => true
                ])

然后使用js脚本,我获得数据属性的内容并显示它。

document.addEventListener("DOMContentLoaded", () => {
        document.querySelectorAll('[data-comment]').forEach(function(field) {
        var comment = field.getAttribute('data-comment');
        var span = document.createElement('div');
        span.classList.add("comment");
        span.innerHTML = comment;
        field.parentNode.appendChild(span);
      }); 
    });

谢谢你的洞察力。

4c8rllxm

4c8rllxm1#

是的,您可以直接在formType中执行此操作,无需JavaScript

use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;

    ->add('funding', ChoiceType::class, [
        'choices'  => [
            'choice 1' => 'aa',
            'choice 2' => 'bb',
            'choice 3' => 'cc',
         ],
      ])

public function finishView(FormView $view, FormInterface $form, array $options): void
{
    $comments[0] = 'Comment 1';
    $comments[1] = 'comment 2';
    $comments[2] = 'comment 3';

    $choices = $view->children['funding']->vars['choices'];

    foreach ($choices as $index => $choice){
        $choice->label .= " ".$comments[$index];
    }
}

结果是:

    • 解决方案2基于帖子所有者评论:**
// formType

        ->add('funding', ChoiceType::class, [
            'mapped' =>false,
            'choices'  => [
                'choice 1' => 'aa',
                'choice 2' => 'bb',
                'choice 3' => 'cc',
                'choice 4' => 'dd',
            ],
            'expanded' => true
        ])

 // in controller

    $comments[0] = 'Lorem ipsum dolor sit amet,consectetur adipiscing elit. Praesent nec pellentesque velit, ut porta erat. Etiam convallis rhoncus erat vitae sodales. Vivamus porta tellus erat, non congue urna blandit eget. Nam urna dolor, lacinia luctus ex sed, ornare luctus eros. Morbi varius est a nisi facilisis, bibendum dictum turpis maximus.';
    $comments[1] = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent nec pellentesque velit, ut porta erat. Etiam convallis rhoncus erat vitae sodales. Vivamus porta tellus erat, non congue urna blandit eget. Nam urna dolor, lacinia luctus ex sed, ornare luctus eros. Morbi varius est a nisi facilisis, bibendum dictum turpis maximus.';
    $comments[2] = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent nec pellentesque velit, ut porta erat. Etiam convallis rhoncus erat vitae sodales. Vivamus porta tellus erat, non congue urna blandit eget. Nam urna dolor, lacinia luctus ex sed, ornare luctus eros. Morbi varius est a nisi facilisis, bibendum dictum turpis maximus.';

    return $this->render('template_name.html.twig', [
        'form' => $form->createView(),
        'comments'=> $comments
    ]);
}        

// twig

<div style="margin: 50px">
    {{ form_label(form.funding) }}
    {% for key, fundingItem in form.funding.children %}
        <div style="padding: 12px">
            {{ form_widget(fundingItem) }}
            {{ form_label(fundingItem) }}
            {% if  key in comments|keys  %}
                <div style="padding: 5px">
                    {{ comments[key]|raw }}
                </div>
            {% endif %}
        </div>
    {% endfor %}
</div>

结果为:

相关问题