如何阻止Symfony 2复选框返回null

时间:2015-06-24 14:34:11

标签: symfony checkbox symfony-forms

在我的Symfony 2表单上,我有2个复选框。文档说明

  

"如果选中此框,则该字段将设置为true(如果该框为)   未选中,该值将设置为false"

这就是我想要发生的事情,但是如果取消选中该复选框,我实际上什么都没有回来。我意识到HTML表单上的复选框通常会在未选中时返回任何内容,我通常会在后端添加一些逻辑来处理它,但听起来Symfony应该为我做这些并且不是。< / p>

我做错了什么或者我错过了什么?

我的表单类型如下:

public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('fullname', 'text')
            ->add('screened', 'checkbox', array(
                'label'     => 'Enable screening emails?' ))
            ->add('dedupe', 'checkbox', array(
                'label'     => 'Enable live dedupeing?'))
 );)

并且twig模板的相关位看起来像这样:

<div class="form-group">
    <div class="input-group col-sm-offset-2 col-sm-10">
        <div class="checkbox col-sm-6">
            {{ form_widget(form.screened) }}
            {{ form_label(form.screened) }}
        </div>
        <div class="checkbox col-sm-6">
            {{ form_widget(form.dedupe, {'attr': {'required': 'false'}}) }}
            {{ form_label(form.dedupe) }}
        </div>

    </div>
</div>

表格在控制器中创建:

/**
     * Creates a new User entity.
     *
     */
    public function createAction(Request $request)
    {
        $entity = new User();
        $form = $this->createCreateForm($entity);
        $form->handleRequest($request);

        if ($form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $this->get('project_login.user_manager')->setUserPassword($entity, $form->get('password')->getData());

            $em->persist($entity);
            $em->flush();

            return $this->redirect($this->generateUrl('user_show', array('id' => $entity->getId())));
        }

        return $this->render('ProjectLoginBundle:User:new.html.twig', array(
            'entity' => $entity,
            'form'   => $form->createView(),
        ));
    }

    /**
     * Creates a form to create a User entity.
     *
     * @param User $entity The entity
     *
     * @return \Symfony\Component\Form\Form The form
     */
    private function createCreateForm(User $entity)
    {
        $form = $this->createForm(new UserType(), $entity, array(
            'action' => $this->generateUrl('user_create'),
            'method' => 'POST', 'validation_groups' => array('create')
        ));

        $form->add('submit', 'submit', array('label' => 'Create'));

        return $form;
    }

1 个答案:

答案 0 :(得分:0)

如果我理解得很清楚,您需要在未选中该字段时进行处理,因为您在处理表单请求时未收到回复

你可以用这种方式解决它:

 if ($form->isValid()) {
     $form->handleRequest($request);

     $em = $this->getDoctrine()->getManager();

     $entity = $form->getData();

     var_dump($entity->getScreened());
     var_dump($entity->getFullname());
     var_dump($entity->getDedupe());

     // They should return you the values set on the create form
     $em->persist($entity);
     $em->flush();
 }

如果它没有以这种方式尝试值,但不是正确的...

if (!isset($form->get('screened')) {
    $entity->setScreened(false);
}

if (!isset($form->get('dedupe')) {
    $entity->setDedupe(false);
}

它会检查您是否未收到重复数据删除的任何数据并从表单中筛选出来,如果是这种情况您知道该值尚未检查