简体   繁体   English

在树枝中循环以形成symfony2

[英]Loop in twig for form symfony2

I prog one application for my project. 我为我的项目编写了一个应用程序。

I have one form for Mesure. 我有一种Mesure表格。

for the moment I have this : 目前我有这个:

在此处输入图片说明

But I want this : 但是我想要这个:

在此处输入图片说明

Have you got a suggestion? 你有建议吗?

My code is : 我的代码是:

MesureType : MesureType:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
    ->add('date')
    ->add('mesure')
    ->add('stagiaire', EntityType::class, array('class' => 'stbfAdministratifBundle:Stagiaire',
    'choice_label' => function($allChoices, $currentChoiceKey)
    {
        return $allChoices->getNom().' '.$allChoices->getPrenom();
    }
    ,'multiple' => false));
    $builder->add('enregistrer', SubmitType::class);
}

My controler : 我的控制者:

public function ajouterAction(Request $request)
    {
    $repository = $this->getDoctrine()
                ->getManager()
                ->getRepository('stbfSportifBundle:ParamPhysiologique');

        $params = $repository->findAll();

        $uneMesure = new Mesure();
        $form = $this->get('form.factory')->create(MesureType::class, $uneMesure);


        if ($request->isMethod('POST') && $form->handleRequest($request)->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $em->persist($uneMesure);
            $em->flush();

        }
        return $this->render('stbfSportifBundle:Mesure:ajouter.html.twig', array('form' => $form->createView(), 'param' => $params));
    }

And twig : 和树枝:

{% block body %}
<h1>Page d'ajout d'une mesure pour un stagiaire</h1>

<table>
<tr>
<td>{{ form_widget(form.date) }}</td>
</tr>
<tr>
<td>{{ form_widget(form.stagiaire) }}</td>
</tr>
<tr>
    {% for param in param %}
<td>
    {{param.libelle}} : {{ form_widget(form.mesure) }}</td>
</tr>
{% endfor %}
</table>
{{ form_widget(form.enregistrer, {'attr': {'class': 'btn btn-primary'}}) }}
{% endblock %}

I need you're help, I don't know why my loop don't work. 我需要您的帮助,我不知道为什么我的循环不起作用。

Thank's for you're help. 谢谢您的帮助。

With reference to your code: first of all try to dump your variable param if you got all the values then change the loop variable ie try: {% for param2 in param %} 参考您的代码:首先,如果获得了所有值,则尝试转储变量param ,然后更改循环变量,即尝试: {% for param2 in param %}

One more thing: your code is 还有一件事:您的代码是

{% block body %}
<h1>Page d'ajout d'une mesure pour un stagiaire</h1>

<table>
<tr>
<td>{{ form_widget(form.date) }}</td>
</tr>
<tr>
<td>{{ form_widget(form.stagiaire) }}</td>
</tr>
<tr>
    {% for param in param %}
<td>
    {{param.libelle}} : {{ form_widget(form.mesure) }}</td>
</tr>
{% endfor %}
</table>
{{ form_widget(form.enregistrer, {'attr': {'class': 'btn btn-primary'}}) }}
{% endblock %}

your table structure is not correct. 您的表结构不正确。 Repeat the inside the for loop. 重复内部for循环。

Use this: 用这个:

{% for param2 in param %}
<tr>
<td>
    {{param2.libelle}} : {{ form_widget(form.mesure) }}</td>
</tr>
{% endfor %}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM