2017-01-16 27 views
0

我有簡單的編輯表單,用戶必須填寫必填字段。問題是沒有顯示必填字段的驗證,並且字段或數據也不會更改。使用Symfony 3更新數據

UPDATE我的var_dump的$form->isSubmitted(),它顯示bool(false)

這裏是我的控制器:

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

這是我的樹枝模板:

{% 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

請確保你告訴我們:發生了什麼,你想要發生什麼,以及你曾經嘗試過什麼,所以我們可以幫助你! –

回答

0

只有一個編輯/更新動作記錄

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

,改變在樹枝一樣一編輯操作的形式作用

確保這是對你有用。

+0

print:var_dump($ form-> getErrorsAsString());並檢查什麼是缺失和哪個字段無效。 –