2017-01-16 2 views
0

Ich habe einfache Bearbeitungsformular, wo der Benutzer erforderliche Felder ausfüllen muss. Das Problem ist, dass die Validierung der erforderlichen Felder nicht angezeigt wird und auch die Felder oder Daten nicht geändert werden.Aktualisieren Sie Daten mit Symfony 3

UPDATE ich var_dump die $form->isSubmitted() und es zeigt bool(false)

Hier ist mein Controller:

public function editAction(Request $request, $id) { 

    $company = $this->getDoctrine() 
     ->getRepository('SwipeBundle:Company') 
     ->find($id); 

    if(!$company) { 
     throw $this->createNotFoundException(
      'No Company found for id '.$id 
     ); 
    } 

    $form = $this->createForm(CompanyType::class, $company, array(
     'action'=>$this->generateUrl('swipe_backend_company_edit', array('id'=>$company->getId())), 
     'method'=>'PUT' 
    ));  

    if ($request->getMethod() == "POST") { 

      if ($form->isSubmitted()) { 

       // $em->persist($company); 
       // $em->flush(); 
       echo "Update"; 
      } 
     } 


    return $this->render('Backend/Company/edit.html.twig', array(
     'form'=>$form->createView(), 
     'company'=>$company 
    )); 
} 

Und hier ist mein Zweig Vorlage:

{% extends '::Backend/base.html.twig' %} 
{% block body %} 

    <!-- Section --> 
    <section class="sections"> 

     <!-- Side Bar --> 
     {% include '::Backend/side_menu_bar.html.twig' %} 

     <!-- Wrapper --> 
     <div id="administrator" class="wrapper"> 

      <div class="mt40 pt30"> <!-- Container --> 
       <h1 class="mb10 bold">Edit Company</h1> 
       <p class="mb30">Fill up all the required fields for Company.</p> 

       {% if not form.vars.valid %} 
        <p class="alert note-error"> 
         There are errors in your form. Please check the fields marked in red. 
        </p> 
       {% endif %} 

       <div class="alert note-error"> 
        <p>Fields with asterisk (*) are required</p> 
       </div> 

       {% set url = path('swipe_backend_company_edit', { 'id': company.id }) %} 

       <form novalidate method="post" action="{{ url }}" class="p20 card mb30"> 

        <div class="sections pb30 pt10"> 
         <fieldset class="col span6">      
          {% set attr = {} %} 
          {% if form_errors(form.name) is not empty %} 
           {% set attr = attr|merge({ 'class': 'alert error'}) %} 
          {% endif %} 
          <label for="" class="input-required"> 
           <strong>Company Name<span class="highlight-red">*</span> 
           </strong> 
          </label> 
          {{ form_widget(form.name, { 'attr': attr }) }} 
          {% if not form.name.vars.valid %} 
          <p class="mt10" style="color: #DC2B1B;"> 
           {{ form.name.vars.errors[0].message }} 
          </p> 
          {% endif %} 
         </fieldset> 

         <fieldset class="col span6"> 
          <strong> 
           {{ form_label(form.website) }} 
          </strong> 
          {{ form_widget(form.website) }} 
         </fieldset> 
        </div> 

        <div class="sections pb30 pt10"> 
         {% set attr = {} %} 
         {% if form_errors(form.email_address) is not empty %} 
          {% set attr = attr|merge({ 'class': 'alert error'}) %} 
         {% endif %} 
         <label for="" class="input-required"> 
          <strong>Company Email Address<span class="highlight-red">*</span> 
          </strong> 
         </label> 
         {{ form_widget(form.email_address, { 'attr': attr }) }} 
         {% if not form.email_address.vars.valid %} 
         <p class="mt10" style="color: #DC2B1B;"> 
          {{ form.email_address.vars.errors[0].message }} 
         </p> 
         {% endif %} 
        </div> 

        <div class="sections pb30 pt10"> 
         <fieldset class="col span6"> 
          <strong> 
           {{ form_label(form.telephone_no) }} 
          </strong> 
          {{ form_widget(form.telephone_no) }} 
         </fieldset> 

         <fieldset class="col span6"> 
          <strong> 
           {{ form_label(form.mobile_no) }} 
          </strong> 
          {{ form_widget(form.mobile_no) }} 
         </fieldset> 
        </div> 

        <div class="sections pb30 pt10"> 
         {% set attr = {} %} 
         {% if form_errors(form.address) is not empty %} 
          {% set attr = attr|merge({ 'class': 'alert error'}) %} 
         {% endif %} 
         <label for="" class="input-required"> 
          <strong>Company Address<span class="highlight-red">*</span> 
          </strong> 
         </label> 
         {{ form_widget(form.address, { 'attr': attr }) }} 
         {% if not form.address.vars.valid %} 
         <p class="mt10" style="color: #DC2B1B;"> 
          {{ form.address.vars.errors[0].message }} 
         </p> 
         {% endif %} 
        </div> 

        <div class="text-right mt20 mb10"> 
         <button class="btn btn-positive mt10 mr5">Update Company</button> 
         <button class="btn btn-positive mt10 mr5">Cancel</button> 
        </div>     

        {{ form_rest(form) }} 
       </form> 

      </div> <!-- Container End --> 

     </div> <!-- Wrapper End --> 

    </section> <!-- Section End --> 

{% endblock %} 
+0

Bitte stellen Sie sicher, dass Sie uns sagen: Was passiert, was Sie wollen und was Sie versucht haben, damit wir Ihnen helfen können! –

Antwort

0

nur eine Aktion zum Bearbeiten/Update die Aufzeichnung

public function editAction($id) { 

    $em = $this->getDoctrine()->getManager();   
    $company = $em 
     ->getRepository('SwipeBundle:Company') 
     ->find($id); 

    if(!$company) { 
     throw $this->createNotFoundException(
      'No Company found for id '.$id 
     ); 
    } 

    $form = $this->createForm(CompanyType::class, $company, array(
     'action'=>$this->generateUrl('swipe_backend_company_update', array('id'=>$company->getId())), 
     'method'=>'PUT' 
    )); 

    if ($request->getMethod() == "POST") { 

     if ($form->isValid()) { 

      $em->persist($company); 
      $em->flush(); 
     } 
    } 

    return $this->render('Backend/Company/edit.html.twig', array(
     'form'=>$form->createView(), 
     'company'=>$company 
    )); 
} 

und ändern auch die Form Aktion in Zweig derselben eine Bearbeitungsaktion

Stellen Sie sicher, dies ist nützlich für Sie.

+0

print: var_dump ($ form-> getErrorsAsString()); und prüfen, was fehlt und welches Feld ungültig ist. –