如何使用1表单保存表单symfony2

时间:2015-05-26 17:02:25

标签: php html forms symfony

我需要保存1张表格,4次,看看......

这是我的表格

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('option', 'text', array('required' => true,  'label' => 'Option', 'translation_domain' => 'Admin',));
    $builder->add('isCorrect', 'checkbox', array('value' => 1,'required' => false,  'label' => 'Is Correct Answer', 'translation_domain' => 'Admin','attr' => array('class' => 'mutuallyexclusive')));
}

在我的控制器中我有......

public function newAction()
{
    $section = $this->getRequest()->get('section');

    $entityA  = new PretestQuestion();
    $entityOption1  = new PretestQuestionOption();
    $entityOption2  = new PretestQuestionOption();
    $entityOption3  = new PretestQuestionOption();
    $entityOption4  = new PretestQuestionOption();

    $formA = $this->createForm(new PretestQuestionType(),$entityA);
    $formOption1 = $this->createForm(new PretestQuestionOptionType(),$entityOption1);
    $formOption2 = $this->createForm(new PretestQuestionOptionType(),$entityOption2);
    $formOption3 = $this->createForm(new PretestQuestionOptionType(),$entityOption3);
    $formOption4 = $this->createForm(new PretestQuestionOptionType(),$entityOption4);

    return array(
        'entityA' => $entityA,
        'formA'   => $formA->createView(),
        'entityOption1' => $entityOption1,
        'formOption1'   => $formOption1->createView(),
        'entityOption2' => $entityOption2,
        'formOption2'   => $formOption2->createView(),
        'entityOption3' => $entityOption3,
        'formOption3'   => $formOption3->createView(),
        'entityOption4' => $entityOption4,
        'formOption4'   => $formOption4->createView(),
        'section' => $section,
    );
}

并保存我的createAction

$entityA  = new PretestQuestion();


    $entityA->setCourse($course);
    $entityOption1  = new PretestQuestionOption();
    $entityOption2  = new PretestQuestionOption();
    $entityOption3  = new PretestQuestionOption();
    $entityOption4  = new PretestQuestionOption();

    $entityOption1->setPretestQuestion($entityA);
    $entityOption2->setPretestQuestion($entityA);
    $entityOption3->setPretestQuestion($entityA);
    $entityOption4->setPretestQuestion($entityA);

    $entityA->setActive(1);

    $formA = $this->createForm(new PretestQuestionType(),$entityA);
    $formOption1 = $this->createForm(new PretestQuestionOptionType(),$entityOption1);
    $formOption2 = $this->createForm(new PretestQuestionOptionType(),$entityOption2);
    $formOption3 = $this->createForm(new PretestQuestionOptionType(),$entityOption3);
    $formOption4 = $this->createForm(new PretestQuestionOptionType(),$entityOption4);


    $formA->bind($request);
    $formOption1->bind($request);
    $formOption2->bind($request);
    $formOption3->bind($request);
    $formOption4->bind($request);
   if ($formA->isValid() and $formOption1->isValid() and $formOption2->isValid() and $formOption3->isValid() and $formOption4->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $em->persist($entityA);
        $em->persist($entityOption1);
        $em->persist($entityOption2);
        $em->persist($entityOption3);
        $em->persist($entityOption4);
        $em->flush();

        return $this->redirect($this->generateUrl('sol90_pretest_admin_list', array('section' => $section, 'type' => 'pretestQuestion')));
    }

在我看来我有这个......

<td>{{ form_widget(formOption1) }}</td>
<td>{{ form_widget(formOption2) }}</td>
<td>{{ form_widget(formOption3) }}</td>
<td>{{ form_widget(formOption4) }}</td>

当我点击保存时,结果是4次相同,最后一次输入的值是4次

帮助!!

1 个答案:

答案 0 :(得分:1)

你对自己这么努力。 Symfony具有内置结构来处理表单的collections - 不需要一次枚举一个子实体/表单。

首先,修改表单构建器以添加此新类型

// Assumes that your PretestQuestion entity has an $options
// field which is a many-to-one for PretestQuestionOption entities
$builder->add('options', 'collection', array(
    'type' => new PretestQuestionOptionType()
));

然后,设置您的初始实体有四个选项(我在这里做一些关于您的实体设计的假设)

$question = new PretestQuestion();
for ($i=0; $i < 4; $i++)
{
  // Assuming PretestQuestion::addOption() does the proper thing
  $question->addOption(new PretestQuestionOption());
}

你的其余行动变得更加简单

$question->setCourse($course);
$question->setActive(1);

$form = $this->createForm(new PretestQuestionType(), $question);


$form->bind($request);
if ($form->isValid()) {
    $em = $this->getDoctrine()->getManager();
    $em->persist($form->getData());
    $em->flush();

    return $this->redirect($this->generateUrl('sol90_pretest_admin_list', array('section' => $section, 'type' => 'pretestQuestion')));
}

你的观点必须像这样修改

{% for option in form.options %}
  <td>{{ form_widget(option) }}</td>
{% endfor %}

现在,您不仅没有摆脱所有错误和头痛,而且子实体/表单的数量是完全动态的。您所要做的就是更改循环终止符的大小。