【问题标题】:Disable HTML5 form validation in Grails Project在 Grails 项目中禁用 HTML5 表单验证
【发布时间】:2013-08-15 04:49:30
【问题描述】:

我已经开始在 Grails 中设计和实现一个博客主页,以练习 Grails 开发和 HTML。我对 HTML4/5 还不是很熟悉。

我的问题是我想禁用 HTML5 的表单验证,标准消息是:“请填写此字段”,如果该字段是必填字段但未填写,而是使用我自己的自定义错误文本,可以输入文件 i18n/messages.properties。

我已经阅读了这两个关于如何使用novalidate=""autocomplete="off" 在纯 HTML5 中禁用表单验证的问题

我已经在我的 Grails 项目中生成了脚手架模板,方法是输入:install-templates

我的计划是更改 _form.gsp 以在方法 renderFieldForProperty() 中包含 autocomplete="off"novalidate="",但两者都不起作用。

希望有人解决了这个问题并希望分享知识;)

编辑:来自脚手架的 renderFieldForProperty() 代码:

private renderFieldForProperty(p, owningClass, prefix = "") {
    boolean hasHibernate = pluginManager?.hasGrailsPlugin('hibernate')
    boolean display = true
    boolean required = false
    if (hasHibernate) {
        cp = owningClass.constrainedProperties[p.name]
        display = (cp ? cp.display : true)
        required = (cp ? !(cp.propertyType in [boolean, Boolean]) && !cp.nullable && (cp.propertyType != String || !cp.blank) : false)
}
    if (display) { %>
<div class="fieldcontain \${hasErrors(bean: ${propertyName}, field: '${prefix}${p.name}', 'error')} ${required ? 'required' : ''}"> <-- At the end of this line i have tried the to attributes mentioned above
<label for="${prefix}${p.name}">
    <g:message code="${domainClass.propertyName}.${prefix}${p.name}.label" default="${p.naturalName}" />
    <% if (required) { %><span class="required-indicator">*</span><% } %>
</label>
${renderEditor(p)}
</div>
<%  }   } %>

如果你向右滚动,我已经写了我尝试过我在帖子中提到的 2 个属性的地方。

【问题讨论】:

  • 请出示您的renderFieldForProperty() 代码或您尝试过但没有奏效的任何其他代码。

标签: html forms validation grails


【解决方案1】:

你需要自定义renderEditor.template这个文件负责根据Domain Class渲染表单域。例如,我更改了isRequired() 方法:

private boolean isRequired() {
  //!isOptional()
  return false //always return false, not including the required='' in the field.
}

【讨论】:

  • 这解决了我的问题,我现在可以再次在 grails 中自定义我的错误消息。感谢您的帮助:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-08
  • 2017-06-01
  • 2012-07-20
相关资源
最近更新 更多