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

sparkyspider picture sparkyspider · Dec 26, 2012 · Viewed 21.1k times · Source

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.

Answer

sparkyspider picture sparkyspider · Dec 26, 2012

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