Search code examples
grailsgrails-ormgrails-domain-classcustomvalidator

Grails - Cannot add a custom validator for a property in the domain class


I am trying to add a custom validator for the String state, which should check if the string country is "usa", then the state should be "Other". If country is not "usa" and state is "other" then it should throw an error.

Also, I would like to add a custom validator for country to do the same.

Please find the code for my domain class below.

package symcadminidp

import java.sql.Timestamp

import groovy.transform.ToString

@ToString
class Account {

static auditable = [ignore:['dateCreated','lastUpdated']]

String organization
String organizationUnit 
String status
String address1
String address2
String zipcode
String state
String country

Timestamp dateCreated
Timestamp lastUpdated

Account(){
    status = "ENABLED"
}


static hasMany = [samlInfo: SAMLInfo, contacts: Contact]
static mapping = {
    table 'sidp_account_t'
    id column: 'account_id', generator:'sequence', params:[sequence:'sidp_seq']
    contacts cascade:'all'
    accountId generator:'assigned'

    organization column:'org'
    organizationUnit column:'org_unit'
    zipcode column:'zip'
    dateCreated column:'date_created'
    lastUpdated column:'date_updated'
}
static constraints = {
    organization size: 1..100, blank: false
    organizationUnit size: 1..100, blank: false, unique: ['organization']
    //The organizationUnit must be unique in one organization 
    //but there might be organizationUnits with same name in different organizations, 
    //i.e. the organizationUnit isn't unique by itself.
    address1 blank:false
    zipcode size: 1..15, blank: false
    contacts nullable: false, cascade: true
    status blank:false
    //state ( validator: {val, obj ->  if (obj.params.country.compareTocompareToIgnoreCase("usa")) return (! obj.params.state.compareToIgnoreCase("other"))})
        //it.country.compareToIgnoreCase("usa")) return (!state.compareToIgnoreCase("other"))}
}
}

When I tried adding the above commented out code, I got the following error:

URI: /symcadminidp/account/index Class: groovy.lang.MissingPropertyException Message: No such property: params for class: symcadminidp.Account

I am new to grails and groovy and would appreciate any help with this issue.


Solution

  • The second value to your validator ( obj ) is the Account domain class.

    A custom validator is implemented by a Closure that takes up to three parameters. If the Closure accepts zero or one parameter, the parameter value will be the one being validated ("it" in the case of a zero-parameter Closure). If it accepts two parameters the first is the value and the second is the domain class instance being validated.

    http://grails.org/doc/latest/ref/Constraints/validator.html

    Your validator should be something like

    state validator: { val, obj -> 
        return ( obj.country.toLowerCase() == 'usa' ) ?
               ( val.toLowerCase() != 'other' ) : 
               ( val.toLowerCase() == 'other' ) 
    }