gpt4 book ai didi

html - "NotBlank"约束上的验证组使字段在未使用时具有 "required"属性

转载 作者:行者123 更新时间:2023-11-28 00:58:54 25 4
gpt4 key购买 nike

我有一个实体字段如下:

/**
* @ORM\Column(nullable=true)
* @Assert\NotBlank(groups={"myGroup"})
*/
protected $field;

然后我调用没有任何验证组的实体表单(我的 configureOptions 函数只设置 data_class)。但是该字段是使用 HTML 中的 required="required" 属性创建的:

<input id="entity_field" name="entity[field]" required="required" type="text">

但是如果我摆弄 HTML 并取出 required="required",验证将在后端按预期工作(即 NotBlank 不适用对于此表格)。

最佳答案

注解@Assert\NotBlank(groups={"myGroup"})是服务器端相关的,输入属性required="required"在构造中定义你的形式。如果不需要此字段,您应该在表单中将必需选项定义为 false,请查看文档 herehere

The required Option

The most common option is the required option, which can be applied to any field. By default, the required option is set to true, meaning that HTML5-ready browsers will apply client-side validation if the field is left blank. If you don't want this behavior, either disable HTML5 validation or set the required option on your field to false:

Also note that setting the required option to true will not result in server-side validation to be applied. In other words, if a user submits a blank value for the field (either with an old browser or web service, for example), it will be accepted as a valid value unless you use Symfony's NotBlank or NotNull validation constraint.

In other words, the required option is "nice", but true server-side validation should always be used.

关于html - "NotBlank"约束上的验证组使字段在未使用时具有 "required"属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43142645/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com