如何在 Grails 中创建带有自定义错误消息的自定义验证器? [英] How do I create a custom validator with a custom error message in Grails?

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

问题描述

如何在 grails 中创建自定义验证器并使其将自定义字符串传递回视图和错误消息?

我看到很多人陷入了同样的困境,我决定创建一个问题和一个答案来解决它.

解决方案

第 1 步:创建域对象或命令对象并在其上放置自定义约束,因此...

@grails.validation.Validateable类人命令{字符串名字字符串姓氏字符串电子邮件地址字符串编号静态约束 = {名字(空白:假)姓氏(空白:假)电子邮件地址(空白:false)idNumber(空白:false,大小:13..16,验证器:{值,对象 ->if (!value.startsWith('77')) 返回假;})}}

这将检查您输入的 idNumber 是否以 77 开头.如果不是,则会发送错误.下一个问题,我们如何显示错误?

第 2 步:自定义视图以显示您的错误

<label class="control-label">身份编号</label><input value="${fieldValue(bean:person,field:'idNumber')}" name="idNumber" type="text"/><div class="error-messages"><g:renderErrors bean="${command}" as="list" field="idNumber"/>

在上面的例子中,${hasErrors(bean:person,field:'idNumber','error')} 将字符串 error 添加到 html 类如果人物模型对象的 idNumber 字段有错误.此类可用于设置输入样式并显示和/或隐藏 div.error-messages 块.<g:renderErrors bean="${command}" as="list" field="idNumber"/> 将显示可怕的错误消息.

第 3 步:创建性感的自定义消息

<块引用>

重要事项:Grails 不允许您直接向看法.相反,您必须在/i18n/messages.properties(消息包)中声明消息.您可以使用任何您喜欢的键.

在messages.properties

validation.idStartsWith77=您的 ID 号必须以 77 开头

在验证器中

idNumber (blank: false, size: 13..16, validator: {value, object ->返回'validation.idStartsWith77'})

现在将一个引用传递给视图,指向messages.properties中的自定义消息...

第 4 步:破解系统以允许将消息直接传递到视图

这仅适用于专门开发多语言网站的人.

Grails 允许您将自定义参数传递给解析器.您可以在消息文件中引用这些自定义参数.为了欺骗系统,我们可以创建一个自定义消息,其中整个消息是一个自定义参数.因此:

在messages.properties

validation.customRuntimeMessage={3}

在验证器中

idNumber (blank: false, size: 13..16, validator: {value, object ->return ['validation.customRuntimeMessage', '你的ID需要以77开头']})

How do I create a custom validator in grails and make it pass a custom string back to the view and an error message?

I saw so many people getting stuck in the same please, I decided to create a question and an answer to deal with it.

解决方案

Step 1: Create a Domain Object or Command Object and put custom constrains on it, as such...

@grails.validation.Validateable
class PeopleCommand {

    String  firstName 
    String  lastName
    String  emailAddress
    String  idNumber

    static constraints = {
        firstName (blank: false)
        lastName  (blank: false)
        emailAddress  (blank: false)
        idNumber  (blank: false, size: 13..16, validator: {value, object ->
                if (!value.startsWith('77')) return false;
        })
    }
}

This will check if the idNumber you entered starts with 77. If it doesn't, it will send an error. Next question, how do we display the error?

Step 2: Customize the View to Display your Error

<div class="${hasErrors(bean:person,field:'idNumber','error')}">
    <label class="control-label">Identity Number</label>
    <input value="${fieldValue(bean:person,field:'idNumber')}" name="idNumber" type="text"/>
    <div class="error-messages">
        <g:renderErrors bean="${command}" as="list" field="idNumber"/>   
    </div>
</div>

In the above example, ${hasErrors(bean:person,field:'idNumber','error')} will add the string error to the html class if the person model object's idNumber field has an error. This class can be used to style the input and show and/or hide the div.error-messages block. The <g:renderErrors bean="${command}" as="list" field="idNumber"/> will display a horrid error message.

Step 3: Create a Sexy Custom Message

IMPORTANT: Grails does not allow you to send a message directly to the view. Instead, you have to declare the message in /i18n/messages.properties (Message Bundles). You can use any key you like.

In messages.properties

validation.idStartsWith77=Your ID number must start with 77

In the validator

idNumber  (blank: false, size: 13..16, validator: {value, object ->
    return 'validation.idStartsWith77'
})

This now passes a reference to the view, pointing to your custom message in messages.properties...

Step 4: Hack the system to allow passing a message directly to the view

This only applies to people that are specifically not developing a multi-language web site.

Grails allows you to pass custom parameters to the parser. You can refer to these custom parameters within your messages file. To cheat the system, we can create a custom message, where the whole message is one custom parameter. As such:

In messages.properties

validation.customRuntimeMessage={3}

In the validator

idNumber  (blank: false, size: 13..16, validator: {value, object ->
    return ['validation.customRuntimeMessage', 'You need to start your ID with 77']
})

这篇关于如何在 Grails 中创建带有自定义错误消息的自定义验证器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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