在Grails中,如何在字段旁边显示验证错误消息? [英] In Grails, how do I display validation error messages next to the fields?

查看:108
本文介绍了在Grails中,如何在字段旁边显示验证错误消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用于验证的 Grails 2.0.4文档向您展示了如何在顶部显示错误消息如果一个字段无效,该页面以及如何向元素添加一个css类,但它不告诉你如何在字段本身旁边显示错误消息,如下所示:

  ----------------------- 
名称:| |你必须输入一个名字!
-----------------------

如何检索无效字段的特定错误消息,然后将其显示在字段旁边?

解决方案

 < g:renderErrors bean =$ {book}as =listfield =title/> 

如果您指定字段属性,它只会呈现错误(s )为该领域。因此,这是由你来编写HTML。

> < g:if test =$ {bean.hasErrors}>< g:renderErrors bean =$ {book}as =listfield =title/>< / g:if>

它可以像您希望的那样简单或复杂,当我通常喜欢grails插件时,这似乎很简单,没有一个,你有更多的控制标记。


The Grails 2.0.4 documentation for validation shows you how to display error messages at the top of the page and how to add a css class to an element if a field is invalid, but it doesn't tell you how to display the error message next to the fields themselves, something like this:

      -----------------------
Name: |                     |  You must enter a name!
      -----------------------

How do you retrieve the specific error message for an invalid field and then display it next to the field it's for?

解决方案

Actually, the documentation does show how to do this, it just isn't overly clear that this is what they mean:

<g:renderErrors bean="${book}" as="list" field="title"/>

If you specify the field attribute, it will only render error(s) for that field. So then it is just up to you to write the HTML accordingly.

<input type="text" ... /> <g:if test="${bean.hasErrors}"><g:renderErrors bean="${book}" as="list" field="title"/></g:if>

It can get as simple or as complicated as you would like it and while I generally like grails plugins, this just seems simple enough to do without one and you have more control over the markup.

这篇关于在Grails中,如何在字段旁边显示验证错误消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆