1

我目前正在开发一个 grails 应用程序。我在控制器中有两个命令对象(AccountInfoCommand 和 EducInfoCommand)。在我的 EducInfoCommand 上,我想检查 yearGraduated 属性是否早于其验证器约束中设置的birthDate(AccountInfoCommand 的属性)。我将如何做到这一点?

这是我的 AccountInfoCommand 代码:

class AccountDetailsCommand  implements java.io.Serializable {

    String username
    String password
    String confirmPassword
    String emailAddress
    Date birthDate        
}

这是我的 EducInfoCommand 代码:

class EducInfoCommand implements java.io.Serializable {

    Integer graduated 
    EducationLevel educationLevel   
    String schoolName 
    String yearGraduated
    String honorsReceived
}

static constraints = {

    yearGraduated nullable: false, maxSize:4, blank: false,  matches: /([0-9]*)/,
      validator: {
            Date.parse('yyyy',it) <= new Date() ? true : false
      }
}

请帮忙!谢谢!

4

1 回答 1

0

您需要参考 EducInfo 所针对的 AccountDetails。例如,如果您添加了一个用户名字段,EducInfoCommand您可以从中查找帐户详细信息(假设有一个类似于命令对象的 AccountDetails gorm 对象):

class EducInfoCommand implements java.io.Serializable { 
    String yearGraduated
    String username
    // ...
}

static constraints = {
    yearGraduated nullable: false, maxSize:4, blank: false,  matches: /([0-9]*)/,
      validator: { val, obj ->
            Date.parse('yyyy',val) > AccountDetails.findByUsername(obj.username).birthDate
      }
}
于 2012-06-21T19:03:13.670 回答