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

前端 未结 2 1421
半阙折子戏
半阙折子戏 2020-12-05 07:37

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 p

相关标签:
2条回答
  • 2020-12-05 08:19

    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']
    })
    
    0 讨论(0)
  • 2020-12-05 08:34

    The alternative to have custom validation is create custom constraint and wire it with Grails.

    http://www.zorched.net/2008/01/25/build-a-custom-validator-in-grails-with-a-plugin/

    0 讨论(0)
提交回复
热议问题