2012-06-21 87 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