2

所以我有这个表格,我想用 Ajax 提交。通常这很容易做到,但现在它不能以正确的方式处理嵌入式集合,这太糟糕了。

我有一个表格,您可以在其中创建一个包含一系列答案的问题。表单创建如下:

    {{ form_start(form, {'attr':{'class': 'form-horizontal', 'id': 'form'}}) }}
        <div class="form-group">
            {{ form_label(form.question, null, { 'label_attr': {'class': 'col-lg-2 control-label'} }) }}
            <div class="col-lg-5">
                {{ form_widget(form.question, { 'attr': {'class': 'form-control'} }) }}
            </div>
        </div>
        {% for answer in form.answers %}
            <div class="form-group">
                {{ form_label(answer.answer, 'Answer '~loop.index, { 'label_attr': {'class': 'col-lg-2 control-label'} }) }}
                <div class="col-lg-5">
                    {{ form_widget(answer.answer, { 'attr': {'class': 'form-control'} }) }}
                </div>
            </div>
        {% endfor %}
        <div class="col-lg-8"><input class="btn btn-primary pull-right" type="submit" /></div>
    {{ form_end(form) }}

现在要提交表单,我已经准备好这个小 javascript:

$('#form').submit( function() {
    $.ajax({
        type: "POST",
        url: "{{ url('questionCreateSubmit') }}",
        data: $(this).serialize(),
        success: function(data) {
            alert(data); 
        }
    });

    return false; 
});

现在在我的控制器中,我有这个来测试它是否正确处理:

/**
 * @Route("/questions/create/submit", name="questionCreateSubmit")
 */
public function questionCreateSubmitAction(Request $request)
{
    $question = new Question();

    $form = $this->createForm(new QuestionType(), $question);
    $form->handleRequest($request);
    die( var_dump( $form->getData() ) );

但它只设置问题属性并忽略答案的集合,这是返回的:

(
    [id:Acme\DemoBundle\Entity\Question:private] => 
    [question:Acme\DemoBundle\Entity\Question:private] => dfsdf
    [answers:Acme\DemoBundle\Entity\Question:private] => Doctrine\Common\Collections\ArrayCollection Object
    (
        [_elements:Doctrine\Common\Collections\ArrayCollection:private] => Array
            (
            )
    )
)

当我通过转储这些变量来检查 $_POST 变量时,它清楚地显示了一个这样的数组结构:

(
    [question] => Array
        (
            [question] => dfsdf
            [answers] => Array
                (
                    [0] => Array
                        (
                            [answer] => sdfsdf
                        )
                    [1] => Array
                        (
                            [answer] => dsfds
                        )
                    [2] => Array
                        (
                            [answer] => fsdfsdf
                        )
                )
            [_token] => cf8781508d9002504455472d676acb1073888e67
        )
)

有没有一种方法可以自动填充集合,还是我必须自己通过遍历答案数组并将其填充到问题对象中来完成这一切?

帮助将不胜感激!

4

1 回答 1

3

没关系,发现问题了。问题出在我的 QuestionType 中,如下所示:

    $builder->add('answers', 'collection', array(
        'type' => new AnswerType()
    ));

但在我的情况下应该是这样的:

    $builder->add('answers', 'collection', array(
        'type' => new AnswerType(),
        'allow_add' => true,
        'allow_delete' => true
    ));
于 2013-08-06T14:16:46.583 回答