2012-06-21 86 views
1

我目前正在研究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 
     } 
} 

请帮帮忙! 谢谢!

回答

0

您需要一些EducInfo用于AccountDetails的参考。例如,如果您添加一个用户名字段的EducInfoCommand你可以看看该帐户细节从(假设有一个AccountDetails格姆对象,它是类似于命令对象):

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 
     } 
} 
+0

Im很抱歉,这个ISN没有工作。抛出空指针异常错误。 – chemilleX3

+0

您将需要适当地填充用户名字段。为此,您应该添加一个可为null的false对象。 – krock