【问题标题】:Symfony Form required trueSymfony 表单需要 true
【发布时间】:2016-01-21 18:43:45
【问题描述】:

我有表单并且对于字段“名字”使用必需的 true,如何覆盖标准消息 - “请填写此字段”?

        $builder
        ->add('firstname', null, array('label' => 'First Name', 'max_length' => 255, 'required' => true))

在我的实体字段中为 nullable = true。如果我想验证我知道的实体 * @Assert\NotBlank(massage: "custom message")。但是我'需要'=>仅在表单中而不是实体中为真(因为某些api需要这个)而且我的问题是如果我仅在表单中验证字段,如何更改自定义消息

【问题讨论】:

  • 您是在谈论 HTML5 验证,并且该消息在您提交之前就来自浏览器?这是实体的一部分还是独立的形式?
  • 在我的实体字段中为 nullable = true。如果我想验证我知道的实体 * @Assert\NotBlank(massage: "custom message")/ 但我 'required' => true 仅在表单而非实体中(因为某些 api 需要这个)我的问题是如何更改自定义消息如果我仅在表单中验证字段

标签: php symfony formbuilder


【解决方案1】:

要创建自定义 symfony 验证消息,您需要在 YourBundle/Resources/Config/validation.yml 中创建 validation.yml

然后里面验证

  1. 设置YourBundle\Entity\EntityName形式的实体
  2. 要设置验证的属性properties: target: - NotBlank: { message: "Your custom message here" }

这是我如何设置自定义验证的屏幕截图(暂时忽略约束部分,只需检查属性部分以及如何设置自定义验证消息。)

您可能还想在创建表单时使用 novalidate 关闭 html5 验证。

<form id="register-form" name="register-form" action="{{ path('register') }}" method="post" novalidate>

【讨论】:

  • 在我的实体字段中为 nullable = true。如果我想验证我知道的实体 * @Assert\NotBlank(massage: "custom message")/ 但我 'required' => true 仅在表单而非实体中(因为某些 api 需要这个)我的问题是如何更改自定义消息如果我仅在表单中验证字段
猜你喜欢
  • 1970-01-01
  • 2013-11-14
  • 2011-06-02
  • 2019-02-27
  • 1970-01-01
  • 2017-04-10
  • 1970-01-01
  • 1970-01-01
  • 2014-06-06
相关资源
最近更新 更多