Symfony2表单生成器:$ form-> bindRequest($ request)失败了吗?如果有空场

时间:2012-10-23 16:07:15

标签: php forms symfony

奥姆/ YAML

Subject:
    type: entity
    repositoryClass: My\SampleBundle\Repository\SubjectRepository
    id:
        id:
            type: integer
            generator: { strategy: AUTO }
    fields:
        body:
            type: text
    oneToMany:
        choices:
            targetEntity: Choice
            mappedBy: subject
            cascade: ["persist", "remove"]
Choice:
    type: entity
    repositoryClass: My\SampleBundle\Repository\ChoiceRepository
    id:
        id:
            type: integer
            generator: { strategy: AUTO }
    fields:
        choice:
            type: text
    manyToOne:
        subject:
            targetEntity: Subject
            inversedBy: choices
            joinColumn:
                name: subject_id
                referencedColumnName: id

查看

<form action="{{ path('path_create or path_edit', {'id': id}) }}" method="post" {{ form_enctype(form) }}>

    // ...

    <ul id="choice-list" data-prototype="{{ form_widget(form.choices.vars.prototype.choice) | e }}">
        {% for choice in form.choices %}
            <li>
                <div class="errors">
                    {{ form_errors(choice.choice) }}
                </div>
                {{ form_widget(choice.choice) }}
            </li>
        {% endfor %}
    </ul>

    <!-- ## The field can be added by JavaScript. -->
    <span class="add-another-choice">Add another choice</span>
    {{ form_rest(form) }}
    <input type="submit" class="_button" />
</form>

窗体/类型

class SubjectType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('body', 'textarea');
        $builder->add('choices', 'collection', array(
            'type' => new ChoiceType(),
            'options' => array(
                'required'  => false
            ),
            'allow_add' => true,
            'by_reference' => false,
            'allow_delete' => true,
            'prototype' => true
        ));
    }

    // ...
}

class ChoiceType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('choice', 'textarea');
    }

    // ...
}

验证

Subject:
    properties:
        body:
            - NotBlank: ~
            - MinLength: 8
        choices:
            - Valid: ~
Choice:
    properties:
        choice:
            - NotBlank: ~

控制器

public function createAction(Request $request, $id)
{
    $em = $this->get('doctrine')->getEntityManager();

    $subject = new Subject();
    $subject->getChoices()->add(new Choice());

    $form = $this->createForm(new SubjectType(), $subject);

    if ($request->getMethod() == 'POST') {
        $form->bindRequest($request);           //<= error on this point. Why?
                                                // if there is the empty field.
        if ($form->isValid()) {
            return // ...
        }
    }

    return $this->render('MySampleBundle:Subject:create.html.twig', array(
        'form' => $form->createView(),
    ));
}

我尝试了symfony2的收集形式 但是,当Javascript增加字段时,如果有空字段,它将失败 当字段增加时,它在执行验证之前变为错误。 (Catchable Fatal Error :))

我如何才能实现这一目标?


编辑:

如果设置了值,则两次或多次注册将成功 如果有一个未设置该值的项目,则会出现两个或多个注册错误 bindRequest函数失败 如果只有空值的一部分添加实体,那么它就会成功 但是,它似乎不是正确的方式。

1 个答案:

答案 0 :(得分:1)

从选择表单字段中删除required = false或将nullable = true添加到架构选择字段