在我的Player域类中,我试图在自定义验证器中使用动态生成的findByNumber方法来查找具有相同编号的现有Player实例。完整的域类如下。
当我运行我的Grails应用程序并有目的地尝试使用生成的脚手架网页创建新播放器时,会抛出以下异常:
Class: groovy.lang.MissingMethodException
Message: No signature of method: com.sciotofootball.Player.findByNumber() is applicable for argument types: (java.lang.Integer) values: [15]
是否允许在自定义验证器中使用动态生成的findBy方法?
class Player {
// Min and max values
static final MIN_GRADE = Holders.config.player.grade.min.toInteger()
static final MAX_GRADE = Holders.config.player.grade.max.toInteger()
static final MIN_NUMBER = Holders.config.player.number.min.toInteger()
static final MAX_NUMBER = Holders.config.player.number.max.toInteger()
static final MIN_WEIGHT = Holders.config.player.weight.min.toInteger()
static final MAX_WEIGHT = Holders.config.player.weight.max.toInteger()
// This pattern matches as follows
// Feet (5 or 6)-Inches (0-12)
static final HEIGHT_PATTERN = "[56]\\-(([0-9]{1})|([12]{1}[0-1]{1}))"
// Valid football positions
static final VALID_POSITIONS = [
// Offense
"QB", "RB", "WR", "TE", "OL",
// Defense
"DL", "LB", "DB",
// Special teams
"P", "K"
]
Integer number;
String firstName;
String lastName;
Integer grade;
String position1;
String position2;
String height;
Integer weight;
static constraints = {
// unique: true adds unique index to the player table
// number nullable: false, unique: true, min: MIN_NUMBER, max: MAX_NUMBER
number nullable: false, min: MIN_NUMBER, max: MAX_NUMBER, validator: { numberValue ->
Player existingPlayer = findByNumber( numberValue )
return ( existingPlayer == null ?: [ 'unique', existingPlayer.firstName, existingPlayer.lastName ] )
}
firstName blank: false
lastName blank: false
grade nullable: false, range: MIN_GRADE..MAX_GRADE
position1 blank: false, inList: VALID_POSITIONS
position2 nullable: true, inList: VALID_POSITIONS, validator: { position2Value, player ->
// Position 2 cannot be equal to position 1
return ( player.position1 != position2Value ?: [ 'duplicate' ] )
}
height blank: false, matches: HEIGHT_PATTERN
weight nullable: false, min: MIN_WEIGHT, max: MAX_WEIGHT
}
}