2016-03-28 71 views
3

我正在用Symfony2構建一個網站,現在我需要創建一個大問卷。顯示很多表格的行類

對於這一點,我創建了2個表:問題和答案(和我有用戶)

我創造了我的問題表中的所有問題,當用戶回答一個問題,我在表中創建行。

我的問題是,我實際上使用form_rest(form)來顯示錶單,而且這非常醜陋! :)

我想申請就可以了一些CSS ...因爲我有很多的問題(也可以是30至60),我不能這樣做{{ form_widget(form.row, { 'attr': {'class': 'form-control'} }) }}

這裏是我的代碼:

for ($i=0; $i < count($questions); $i++) { 

      $answer = null; 
      for ($j=0; $j < count($answers); $j++) { 

       if ($answers[$j]->getQuestion() == $questions[$i]) { 
        $answer = $answers[$j]; 
        break; 
       } 
      } 

      $tmpForm->add($questions[$i]->getId(), TextType::class, array(
       'required' => false, 
       'label' => $questions[$i]->getQuestion(), 
       'data' => ($answer != null ? $answer->getAnswer() : '')) 
      ); 
     } 

     $form = $tmpForm->getForm(); 

     if ($request->isMethod('POST')) { 

      $form->handleRequest($request); 
      $data = $form->getData(); 

      $em = $this->getDoctrine()->getManager(); 

      for ($i=0; $i < count($questions); $i++) { 

       $value = $data[$questions[$i]->getId()]; 

       if ($value == null) 
        continue; 

       $ans = $answerRepository->findOneBy(array('question' => $questions[$i])); 

       if ($ans != null && $ans->getAnswer() != $value) { 

        $ans->setAnswer($value); 
        $ans->setUpdatedOn(new \Datetime()); 
       } 
       else if ($ans == null) { 

        $ans = new Answer(); 

        $ans->setAnswer($value); 
        $ans->setQuestion($questions[$i]); 
        $ans->setCreatedOn(new \Datetime()); 
        $ans->setUpdatedOn(new \Datetime()); 

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

如何渲染帶有自定義文本框的每一行?

感謝您的幫助

回答

2

設置CSS類FormBuilder

$tmpForm->add(
    $questions[$i]->getId(), 
    TextType::class, 
    array(
     'required' => false, 
     'label' => $questions[$i]->getQuestion(), 
     'data' => ($answer != null ? $answer->getAnswer() : ''), 
     'attr' => array('class' => 'form-control') 
    ) 
); 

如果您需要更多個性化的投入,那麼我會建議form rendering

+0

太容易了......就像一個魅力。謝謝 ! – carndacier