2017-02-21 25 views
1

すべてのオブジェクトを1つのビューに編集したいと思っています。すでに1つのページですべてのエンティティのオブジェクトのフォームを編集するSymfony3

、私はこの効果を持っている:PICTURE

しかし、私は単純なオブジェクトを編集しようとすると、それは動作しません。

Corrections.html.twig

<div class="row"> 
    <div class="col-sm-10 col-sm-offset-1"> 
     <table class="table table-bordered"> 
      <thead> 
       <tr> 
        <th>Nazwa poprawki</th> 
        <th>Status dla</th> 
        <th>Status dla klienta</th> 
        <th>Nazwa projektu</th> 
        <th>Klient</th> 
        <th>Obszar</th> 
        <th>Piorytet</th> 
        <th>Data utworzenia</th> 
        <th>Iteracja</th> 
       </tr> 
      </thead> 
      <tbody> 

       {% for correction in corrections %} 
        {{ form_start(form[loop.index0]) }} 
        <tr> 
         <td>{{correction.correctionName}}</td> 
         <td>{{ form_widget(form[loop.index0].adminStatusCorrectionId) }}</td> 
         <td>{{ form_widget(form[loop.index0].userStatusCorrectionId) }}</td> 
         <td>{{correction.projectId.projectName}}</td> 
         <td>{{correction.projectId.userId.firstName}} {{correction.projectId.userId.lastName}}</td> 
         <td>{{ form_widget(form[loop.index0].areaId) }}</td> 
         {% if correction.priority %} 
          <td>Tak</td> 
         {% else %} 
          <td>Nie</td> 
         {% endif %} 
         <td>{{correction.creationDate|date('Y-m-d')}}</td> 
         <td>{{correction.iteration}}</td> 
         <td>{{ form_widget(form[loop.index0].save) }}</td> 
        </tr> 
        </form> 
        {{ form_end(form[loop.index0]) }} 
       {%endfor %} 

      </tbody> 
     </table> 
    </div> 
</div> 

AdministratorController.php

public function correctionsAction(Request $request) { 
    $repository = $this->getDoctrine()->getRepository('AppBundle:Correction'); 
    $corrections = $repository->findAll(); 

    foreach ($corrections as $key => $value) { 
     $form = $this->createForm(CorrectionType::class, $corrections[$key]); 
     $formView[] = $form->createView(); 
    } 
    $form->handleRequest($request); 

    if ($form->isSubmitted() && $form->isValid()) { 

     $correction = $form->getData(); 
     $em = $this->getDoctrine()->getManager(); 
     $em->persist($correction); 
     $em->flush(); 

     return $this->redirectToRoute('admin_view_corrections'); 
    } 

    return $this->render('administrator/corrections.html.twig', array(
       'corrections' => $corrections, 
       'form' => $formView 
    )); 
} 

CorrectionType.php

class CorrectionType extends AbstractType { 

    public function buildForm(FormBuilderInterface $builder, array $options) { 
     $builder 
       ->add('adminStatusCorrectionId', EntityType::class, array(
        'class' => 'AppBundle:AdminStatusCorrection', 
        'choice_label' => 'statusName' 
       )) 
       ->add('userStatusCorrectionId', EntityType::class, array(
        'class' => 'AppBundle:UserStatusCorrection', 
        'choice_label' => 'statusName' 
       )) 
       ->add('areaId', EntityType::class, array(
        'class' => 'AppBundle:Area', 
        'choice_label' => 'areaName' 
       )) 
       ->add('save', SubmitType::class, array('label' => 'Aktualizacja')) 
     ; 
    } 

    public function configureOptions(OptionsResolver $resolver) { 
     $resolver->setDefaults(array(
      'data_class' => Correction::class, 
     )); 
    } 

} 

今何ができますか?

EDIT

私のフォームの全ては、 "補正" の名前を持っています。

<form name="correction" method="post"></form> 
+0

あなたは私たちにエラーが発生しますか? –

答えて

0

はあなたがcreateNamedを使用できるように、また、あなたは、あなたのフォームのそれぞれに別の名前を与える必要があり、あなたのforeachのサイクル内で取り扱いのフォームを持っている必要があります。私は12件のフォームを持っている。この場合

'form.factory'の()メソッド:

foreach ($corrections as $key => $value) { 
    $formName = 'form_' . $key; 
    $form = $this->get('form.factory')->createNamed($formName, CorrectionType::class, $corrections[$key]); 

    if ($request->getMethod() === 'POST' && $request->request->has($formName)) { 
     $form->handleRequest($request); 
    } 

    if ($form->isSubmitted() && $form->isValid()) { 

     $correction = $form->getData(); 
     $em = $this->getDoctrine()->getManager(); 
     $em->persist($correction); 
     $em->flush(); 

     return $this->redirectToRoute('admin_view_corrections'); 
    } 

    $formView[] = $form->createView(); 
} 
+0

ありがとうございます。 1行を編集すると、すべての行が編集されます。だから今どこに問題があるの? :) – Terixer

+0

'form.factory'のcreateNamed()メソッドの使い方を教えてもらえますか?私は何度も試してみましたが、うまくいきませんでした:/ – Terixer

+0

問題は、すべてのフォームの名前が同じで、handleRequest()メソッドはどのフォームが送信されたのか識別できないことです。私はそれぞれのフォームに異なる名前を設定できるように、 'form.factory'サービスのcreateNamed()メソッドを使用するように答えを更新しました。その後、どのフォームが提出されたかを確認することができます。私の答えを確認し、更新されたコードを確認してください。 –

関連する問題