我有表单,并且对于字段“名字”使用必需的 true,如何覆盖标准消息 - “请填写此字段”?
$builder
->add('firstname', null, array('label' => 'First Name', 'max_length' => 255, 'required' => true))
在我的实体字段中 nullable = true。如果我想验证实体,我知道 * @Assert\NotBlank(massage: "custom message")。但我'required' => true仅在表单中而不是在实体中(因为某些api需要这个)并且我的问题是如果我仅在表单中验证字段
,如何更改自定义消息最佳答案
要创建自定义 symfony 验证消息,您需要创建 validation.yml
类里面YourBundle/Resources/Config/validation.yml
然后在验证里面
- 设置表单
YourBundle\Entity\EntityName
的实体 - 要设置验证的属性
properties: target: - NotBlank: { message: "Your custom message here" }
这是我如何设置自定义验证的屏幕截图(暂时忽略约束部分,只需检查属性部分以及如何设置自定义验证消息。)
您可能还想在创建表单时使用 novalidate 关闭 html5 验证。
<form id="register-form" name="register-form" action="{{ path('register') }}" method="post" novalidate>
关于php - Symfony 表单需要 true,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34931829/