Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 308
sparkyspider Avatar asked Dec 26 '12 09:12

sparkyspider


People also ask

How do you show custom validator error message in validation summary?

In order for the Validator 's error message to display in the ValidationSummary , you need to set the Validator s Display="none" .

What methods should you implement for your custom validator?

Implementing the Validator Interface A Validator implementation must contain a constructor, a set of accessor methods for any attributes on the tag, and a validate method, which overrides the validate method of the Validator interface.

What is a custom validator?

The CustomValidator control is a separate control from the input control it validates, which allows you to control where the validation message is displayed. Validation controls always perform validation on the server.


1 Answers

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']
})
like image 88
sparkyspider Avatar answered Sep 23 '22 06:09

sparkyspider