没有考虑Symfony2表单未选中的复选框,为什么? [英] Symfony2 form unchecked checkbox not taken into account, why?

查看:33
本文介绍了没有考虑Symfony2表单未选中的复选框,为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我发送带有 unchecked 复选框的表单时,如果相关实体属性等于 true ,则它不会更改为 false .

When I send a form with an unchecked checkbox, if the related entity property equals true, then it does not change to false.

反之亦然(将带有选中复选框发送的表单设置为true)以及所有其他字段保存的表单都可以正常工作.

The other way round (setting property to true when the form is sent with a checked checkbox) works fine, as well as all the forms other fields saving.

这是我构建表单并声明相关属性的方式:

Here is how I build the form and declare the related property:

// --- Form creation function EntityType::buildForm() ---
$builder->add('secret', 'checkbox', array( 'required' => false ));

// --- Entity related property, Entity.php file ---
/** @ORM\Column(name="secret", type="boolean") */
protected $secret;


发生此问题是因为使用 PATCH请求提交了表单.


The issue happens because the form is submitted using a PATCH request.

在Symfony中, Form :: submit 方法由请求处理程序使用以下行调用:

In Symfony, the Form::submit method is called by a Request Handler with this line:

$form->submit($data, 'PATCH' !== $method);

结果是,在PATCH请求的情况下, Form :: submit $ clearMissing 参数设置为 false ,因此,将未发送的字段保留为旧值.

As a result the Form::submit $clearMissing parameter is set to false in the case of a PATCH request, thus leaving the non-sent fields to their old value.

但是我不知道如何解决该问题.如果未选中该复选框时,我明确地将JSON {secret:false} 传递给Symfony框架,它将把它解释为"false" 字符串,并认为真实值,因此考虑了选中的复选框...

But I do not know how to solve the problem. If I explicitely pass a JSON {secret: false} to the Symfony framework when the checkbox is not checked, it will interpret it as the "false" string and consider that a true value, thus considering the checkbox checked...

NB.我遇到与完全相同的问题,即使用 choice 字段类型(带有 multiple extended 到链接到Doctrine Simple Array属性的 true ):给定的复选框一旦被发送一次,一旦被发送,就不可能将相关属性重新设置为 false 未选中提交.

NB. I have exactly the same issue with an array of checkboxes using a choice field type (with multiple and extended to true) linked to a Doctrine Simple Array property: as soon as a given checkbox has been sent once as checked, it is impossible to set back the related property to false with subsequent unchecked submissions.

推荐答案

之所以会出现此问题,是因为表单是使用 PATCH请求提交的.

The issue happens because the form is submitted using a PATCH request.

这导致打开此Symfony问题.

如前所述,一种解决方法是在未选中复选框(而不是不发送任何内容)时显式发送特定的保留值(例如字符串'__false'),然后使用自定义数据转换器将其替换为'null'格式为:

As explained, one workaround is to explicitely send a specific reserved value (for instance the string '__false') when the checkbox is unchecked (instead of sending nothing), and replace this value by 'null' using a custom data transformer in the form type:

// MyEntityFormType.php -- buildForm method
$builder->add('mycheckbox', ...);
$builder->get('mycheckbox')
    ->addViewTransformer(new CallbackTransformer(
        function ($normalizedFormat) {
            return $normalizedFormat;
        },
        function ($submittedFormat) {
            return ( $submittedFormat === '__false' ) ? null : $submittedFormat;
        }
    ));

带有"choice"(选择)字段的情况无法以相同的方式解决.它实际上是Symfony的错误,已在此问题中进行了处理.

The case with the 'choice' field can't be solved the same way. It is actually a bug of Symfony, dealt with in this issue.

这篇关于没有考虑Symfony2表单未选中的复选框,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆