In Grails, how do I display validation error messages next to the fields?

梦想的初衷 提交于 2019-12-20 17:39:32

问题


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?


回答1:


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.




回答2:


I use the Grails Fields plugin to do this, and it works a treat.

It makes it easy to create default templates for form field rendering. For example I have the following in grails-app/views/_fields/default/_field.gsp:

<%@ page defaultCodec="html" %>
<div class="control-group${invalid ? ' error' : ''}">
    <label class="control-label" for="${property}${index ?: ""}">${label}</label>
    <div class="controls">
        <%= widget.replace("id=\"${property}\"", "id=\"${property}${index ?: ""}\"") %>
        <g:if test="${invalid}"><span class="help-inline">${errors.join('<br>')}</span></g:if>
    </div>
</div>

As you can see from the HTML the errors are displayed inline. Here is part of my login form:

<g:form controller="home" action="login" >
    <f:field bean="user" property="email"/>
    <f:field bean="user" property="password">
        <g:field type="password" name="${property}" value="${value}"/>
    </f:field>
</g:form>



回答3:


Here is the custom error in context, wrapped around username field. This will do what you want.

<dt>User Id</dt>
            <dd><g:textField name="username" value="${user?.username}"/>
            <g:hasErrors bean="${user}" field="username">
                    <g:eachError bean="${user}" field="username">
                        <p style="color: red;"><g:message error="${it}"/></p>
                    </g:eachError>
                </g:hasErrors>
            </dd>



回答4:


I would recommend going with Jquery validation plugin. There's several Grails plugin about this, but they are a bit out-dated. Besides, I think this task is pretty simple for using another plugin.



来源:https://stackoverflow.com/questions/10943684/in-grails-how-do-i-display-validation-error-messages-next-to-the-fields

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!