Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
479 views
in Technique[技术] by (71.8m points)

validation - How do I create a custom validator with a custom error message in Grails?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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']
})

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...