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

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

问题描述

我如何在grails中创建一个自定义验证器,并将它传递给视图和错误消息?

我看到很多人陷入困境在同一个请求中,我决定创建一个问题和一个答案来处理它。 解决方案

第1步:创建一个域对象或命令对象并将自定义约束放在它上面,比如...

  @ grails.validation.Validateable 
class PeopleCommand {

String firstName
String lastName
String emailAddress
String idNumber

static constraints = {
firstName(blank :false)
lastName(空白:false)
emailAddress(空白:false)
idNumber(空白:false,大小:13..16,验证器:{value,object - >
if(!value.startsWith('77'))return false;
})
}
}

这将检查是否t他输入的idNumber从77开始。如果不是,它会发送一个错误。下一个问题,我们如何显示错误?



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



 < div class =$ {hasErrors(bean:person,field:'idNumber','error')}> 
< label class =control-label>身份证号码< / label>
< input value =$ {fieldValue(bean:person,field:'idNumber')}name =idNumbertype =text/>
< div class =error-messages>
< g:renderErrors bean =$ {command}as =listfield =idNumber/>
< / div>
< / div>

在上例中, $ {hasErrors(bean:person,field: 'idNumber','error')}如果person模型对象的idNumber字段有错误,那么会将字符串 error 添加到html类中。这个类可以用来设置输入风格,并显示和/或隐藏 div.error-messages 块。 < g:renderErrors bean =$ {command}as =listfield =idNumber/> 会显示一个可怕的错误信息。



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




重要 :Grails不允许您直接向
视图发送消息。相反,你必须在/i18n/messages.properties(Message Bundles)中声明消息。您可以使用任何您喜欢的按键。


在messages.properties中

  validation.idStartsWith77 =您的身份证号码必须以77开头b $ b  

在验证器中

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

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



第4步:对系统进行破解,允许直接向消息传递消息 b
$ b

这仅适用于 开发人员一个多语言的网站。



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

在messages.properties中

  validation.customRuntimeMessage = {3} 

验证器

  idNumber(空白:false,大小:13..16,验证器:{value,object  - > 
return ['validation.customRuntimeMessage','您需要以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天全站免登陆