php - Symfony 2.7 - Nested collections form types and choices -
i learning symfony 2 framework , struggling little bit on nested collections form types.
i trying build simple nested collections form entities modeled following:
mysurvey has many myquestions myquestions has many myanswer each myanswer holds possible value linked myquestion
my objective have form displays questions survey , each question display available answer radio buttons (choices). able display answers text fields not choices...
here code:
here entities:
class mysurvey { .... /** * @orm\manytomany(targetentity="myquestion") * @orm\jointable(name="my_survey_questions", * joincolumns={@orm\joincolumn(name="survey_id", referencedcolumnname="id")}, * inversejoincolumns={@orm\joincolumn(name="question_id", referencedcolumnname="id")} * ) */ protected $questions; ... } class myquestion { ... /** * @orm\column(type="string", length=255) */ protected $title; /** * @orm\manytomany(targetentity="myanswer") * @orm\jointable(name="question_answers", * joincolumns={@orm\joincolumn(name="question_id", referencedcolumnname="id")}, * inversejoincolumns={@orm\joincolumn(name="answer_id", referencedcolumnname="id")} * ) */ protected $answers; ... } class myanswer { .... /** * @orm\column(type="string", length=255) */ protected $value; ... }
here form types:
class mysurveyformtype extends abstracttype { public function buildform(formbuilderinterface $builder, array $options) { $builder->add('questions', 'collection', array('type' => new myquestionformtype())); } ... } class myquestionformtype extends abstracttype { public function buildform(formbuilderinterface $builder, array $options) { $builder->add('answers', 'collection', array('type' => new myanswerformtype())); } ... } class myanswerformtype extends abstracttype { public function buildform(formbuilderinterface $builder, array $options) { $builder->add('value'); } ... }
and here template:
{% extends 'base.html.twig' %} {% block body %} <form method="post" {{ form_enctype(form) }}> {{ form_errors(form) }} {% set = 0 %} <ol> {% question in form.questions %} <br /> <li> {{ questions[i].title }}<br /><br /> {% set j = 0 %} {% answer in question.answers %} {{ form_widget(question.answers[j]) }} {% set j = j + 1 %} {% endfor %} {% set = + 1 %} </li> {% endfor %} </ol> <input type="submit" class="btn btn-primary" /> </form> {% endblock %}
i tried change buildform function in myquestionformtype in order select proper answers question. in order solution working need have access answers of each questions, , don't know how done.
public function buildform(formbuilderinterface $builder, array $options) { $builder->add('answers', 'entity', array( 'class' => 'appbundle:myanswer', 'choices' => ??? )); }
if pass survey parameter in controller, there way "the current question" value in myquestionformtype can retrieve answers ?
$form = $this->createform(new mysurveyformtype($survey->getquestions()), $survey);
any appreciated !
thanks lot
Comments
Post a Comment