私は、フォームのコレクション(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関数を持っています。
ありがとうございます。まさに私が見つけようとしていたもの。 誰かが私の他の問題で私を助けることができるかどうかを確認するために少し質問を残しておきます。そうでなければあなたの回答をマークします – p3tch
質問の後半が新しいトピックhttp://stackoverflow.com/questions/に移動しました40237645/symfony-why-does-my-form-get-all-answer-that-in-the-table – p3tch