Grails-无法为域类中的属性添加自定义验证器
我正在尝试为字符串状态添加一个自定义验证器,它应该检查字符串国家是否为“usa”,然后该状态应该为“Other”。若国家不是“美国”,州是“其他”,那个么它应该抛出一个错误 此外,我还想为country添加一个自定义验证器,以便执行相同的操作 请在下面找到我的域类的代码Grails-无法为域类中的属性添加自定义验证器,grails,gorm,grails-domain-class,customvalidator,Grails,Gorm,Grails Domain Class,Customvalidator,我正在尝试为字符串状态添加一个自定义验证器,它应该检查字符串国家是否为“usa”,然后该状态应该为“Other”。若国家不是“美国”,州是“其他”,那个么它应该抛出一个错误 此外,我还想为country添加一个自定义验证器,以便执行相同的操作 请在下面找到我的域类的代码 package symcadminidp import java.sql.Timestamp import groovy.transform.ToString @ToString class Account { stat
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"))}
}
}
当我尝试添加上面注释掉的代码时,出现以下错误:
URI:/symcadminidp/account/index
类:groovy.lang.MissingPropertyException
消息:没有此类属性:类的参数:symcadminidp.Account
我不熟悉grails和groovy,希望您能在这个问题上提供帮助。验证器(obj)的第二个值是Account domain类 自定义验证器由一个闭包实现,该闭包最多需要三个 参数。如果闭包接受零个或一个参数,则 参数值将是正在验证的参数值(在 零参数闭包)。如果它接受两个参数,则第一个参数是 值,第二个是正在验证的域类实例 您的验证器应该类似于
state validator: { val, obj ->
return ( obj.country.toLowerCase() == 'usa' ) ?
( val.toLowerCase() != 'other' ) :
( val.toLowerCase() == 'other' )
}