2016-10-24 26 views
1

私は、フォームのコレクション(Voteには、多くの場合、VoteChoice)を含むフォームを持っています。私のコントローラで今Symfony - プレーンテキストのフォームアイテム

class VoteChoiceType extends AbstractType 
{ 
    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $builder 
      ->add('answer', null, array('disabled' => true)) 
      ->add('priority', null); 
    } 

    public function configureOptions(OptionsResolver $resolver) 
    { 
     $resolver->setDefaults(array(
      'data_class' => 'PollBundle\Entity\VoteChoice', 
     )); 
    } 
} 

を次のようにVoteChoiceTypeは、私が(URL由来)現在のポーリングので

$vote = new Vote(); 
$vote->setPoll($poll); 
foreach ($vote->getPoll()->getPollOptions() as $op) { 
    $vc = New VoteChoice(); 
    $vote->addVoteChoice($vc->setAnswer($op)); 
} 

のために利用可能な選択肢に応じanswerを設定し、作成し、多くのVoteChoicesを移入でありますフォームがロードされ、私はすべてのオプションを表示するだけで、実際の選択ではないようにしておき、ユーザーは必要な優先順位を設定できます。しかし、答えは私が私のpoll_optionsテーブルに持って一つ一つの答えである私はvoteChoiceをしたい

現在の小枝テンプレート

<ul class="voteChoices" data-prototype="{{ form_widget(form.voteChoices.vars.prototype)|e('html_attr') }}"> 
    {% for voteChoice in form.voteChoices %} 
      <li>{{ form_row(voteChoice.answer) }} {{ form_row(voteChoice.priority) }}</li> 
    {% endfor %} 
</ul> 
</div> 
<p><button type="submit" class="btn btn-success">Go!</button></p> 
{{ form_end(form) }} 

(各Pollは、各Voteが多くVoteChoiceを持っているのと同じように多くのPollOptionを、持っています)。私はFormBuilderでそれを無効にすることができますが、ドロップダウンメニューの一部として表示したくありません、私は単純なテキストとしてそれをしたい)

voteChoice.answerを使用すると、次のsymfonyエラーが発生します

An exception has been thrown during the rendering of a template ("Catchable Fatal Error: Object of class Symfony\Component\Form\FormView could not be converted to string") in poll\vote.html.twig at line 9.

私はVoteChoiceクラスに__toString関数を持っています。

答えて

1

I want the voteChoice.answer as a plain text (so it's not part of a dropdown - I know I can disable it in the FormBuilder, but I don't want it to appear as part of a drop-down menu, I just want it as plain text)

あなたはform.vars.valueReference)を経由して、フォームの現在のデータにアクセスすることができます。これはvoteChoice.vars.valuePollBundle\Entity\VoteChoiceのインスタンスであることを意味

{{ voteChoice.vars.value.answer }} 

ので、あなたがあれば安全にあなたのフォームからanswerフィールドを削除することができますこれは編集では必要ありません。

+0

ありがとうございます。まさに私が見つけようとしていたもの。 誰かが私の他の問題で私を助けることができるかどうかを確認するために少し質問を残しておきます。そうでなければあなたの回答をマークします – p3tch

+0

質問の後半が新しいトピックhttp://stackoverflow.com/questions/に移動しました40237645/symfony-why-does-my-form-get-all-answer-that-in-the-table – p3tch

関連する問題