【问题标题】:How display a field error in a Grails-generated form when performing custom validation?执行自定义验证时如何在 Grails 生成的表单中显示字段错误?
【发布时间】:2012-11-24 21:50:57
【问题描述】:

我有一个 Grails 生成的表单,其中包含以下字段:

<div class="fieldcontain ${hasErrors(bean: applicantInstance, field: 'applicantName', 'error')} ">
<label for="applicantName">
    <g:message code="applicant.applicantName.label" default="Applicant Name" />

</label>
<g:textField name="applicantName" maxlength="50" value="${applicantInstance?.applicantName}"/>
</div>

我想在正常的 Grails 验证之外执行服务器端验证,并在验证失败时重新显示记录。我尝试添加自己的 FieldError,但它不起作用。

def error = new org.springframework.validation.FieldError("Applicant", "applicantName", "This name fails the custom server-side validation")
applicantInstance.errors.fieldErrors.add(error)

如何在我的 Grails 生成的表单中显示此字段错误?

【问题讨论】:

    标签: grails


    【解决方案1】:

    如果您想在域对象中声明的成本约束之外添加自定义验证,我建议您使用Command Objects

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-01
      • 1970-01-01
      • 2014-11-06
      • 1970-01-01
      • 2021-10-13
      • 2014-04-24
      • 2012-06-12
      • 2014-12-24
      相关资源
      最近更新 更多