2016-06-13 8 views
1

私のプロジェクトでは、私はSymfonyフレームワークを使用しました。フォームのリストを生成するには、selectオプションを使用する必要があります。ここで twigのオプションhtmlタグを選択Symfony

はコードです:

フォーム:

<form method="post" {{ form_enctype(form)}} action="{{ path('my_path')}}"> 
    {{form_errors(form)}} 
    <div name="nature"> 
     {{form_label(form.nature,"(*) Nature sample")}} 
     {{form_errors(form.nature)}} 
     <select name="nature" id="nature"> 
      <option value ="Other">Other</option> 
      <option value ="Adn">ADN</option> 
     </select> 
    </div> 
    {{ form_widget(form) }} 
    <input type="submit" value="Next" class="btn btn-primary" /> 
</form> 

にformType:

public function buildForm(FormBuilderInterface $builder, array $options){ 
    $builder 
      ->add('nature') 
      ->add('origin'); 
    } 

コントローラー:

public function madeDemandAction($id, Request $request) 
{ 
    $em = $this-> getDoctrine() -> getManager(); 
    $sample = new Sample(); 
    $repository = $this ->getDoctrine() 
       ->getManager() 
       ->getRepository('BsBundle:Demand') 
       ->find($id); 

    $demand = $repository; 
    $form=$this ->createForm(new SampleType, $sample); 

    if($request ->getMethod() == 'POST') 
    { 
     $form->handleRequest($request); 
     if($form->isSubmitted() && $form->isValid()) 
     { 
     dump($request); 
     $inforequest=$form->getData(); 
     dump($inforequest); 
     $em = $this->getDoctrine()->getManager(); 
     $em->persist($inforequest); 
     $em->flush(); 
     return $this->redirect($this->generateUrl('homepage')); 
     } 
    } 
    return $this ->render('bs_connected/human_demand.html.twig' 
    , array('form'=>$form ->createView() 
      , 'inforequest'=>$inforequest 
     )); 
    } 

私は上のオプションを選択したときに問題がある、私のフォーム、フィールドはn私のデータベースの負荷。

+1

フォーム送信を処理するコントローラコードを追加することをお勧めします。 – tlenss

答えて

0

問題が解決しました。私は私の選択オプションIDにここ

コード

<select name="bs_bundle_sample[nature]" id="bs_bundle_sample_nature"> 
     <option value ="Other">Other</option> 
     <option value ="Adn">ADN</option> 
    </select> 

精密小型の正しい名前を入れていない :私のbs_bundle_sampleは私にformTypeの名前(名前は私のgetName関数に見つける。)です。

0

symfonyはすべて自動的にこれを行うことができます:最後に

{{form_errors(form.nature)}} 
{{form_widget(form.nature)}} 

:で選択を置き換える、

 ->add('nature', 'choice', array('label' => '(*) Nature sample', 'choices' => array ('Other' => 'Other', 'Adn' => 'ADN'))) 

そして、あなたの小枝に:にあなたにformTypeに '自然' のオプションを変更してみてください:完全な参照はここにあります:http://symfony.com/doc/current/book/forms.html

+0

私はそれを知っていますが、私は以前の選択(自然)の選択に依存する他のフィールド(起源)を動的に生成するJavascriptスクリプトを使用します。これが私がこれを使う必要がある理由です。 –

1

私は問題がコントローラで始まると思います。コントローラーがgetではなくpostで呼び出された場合、$ inforequestは空です。ユーザーがフォームを投稿していない場合、データはどこから来ますか?

私が知っている限り、Requestオブジェクトは、常に関数呼び出しの最初の変数として注入する必要があります。

これらがソートされている場合、デフォルト値をtwigに設定できるはずです。このようなもの:

<select name="nature" id="nature"> 
     <option value ="Other" {% if inforequest.nature == 'Other' %} selected="selected" {% endif %}>Other</option> 
     <option value ="Adn" {% if inforequest.nature == 'Adn' %} selected="selected" {% endif %}>ADN</option> 
    </select> 
関連する問題