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.
In order for the Validator 's error message to display in the ValidationSummary , you need to set the Validator s Display="none" .
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.
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.
@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?
<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.
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...
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']
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With