如何从另一个命令对象访问命令对象的属性?

时间:2012-06-21 08:16:10

标签: grails properties groovy controllers

我目前正在处理grails应用程序。我在控制器中有两个命令对象(AccountInfoCommand和EducInfoCommand)。在我的EducInfoCommand上,我想检查yearGraduated属性是否早于其验证者约束的set 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
      }
}

请帮忙! 谢谢!

1 个答案:

答案 0 :(得分: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
      }
}