0

我有一堂课:

class PersonCommand implements Validateable {
    String firstName
    String lastName

    static constraints = {
        firstName nullable: true
        lastName nullable: true
    }
}

我有一个我需要验证的 PersonCommand 类型的列表。我想遍历每个元素并检查 firstName 和 lastName 是否都为空。有没有办法在不明确检查这些属性的情况下做到这一点?我想做类似的事情:

for(PersonCommand person in people) {
    if(areAllMapValuesNull(person.properties)) {
        person.validate()
    } else {
        ...

但是 person.properties 添加了其他属性,而不仅仅是 firstName 和 lastName,因为它是 Validateable。我不是在寻求有关 areAllMapValuesNull() 函数的帮助,只是在获取值 firstName 和 lastName 而不对检查进行硬编码。

4

1 回答 1

1

你可以试试这个代码,我没有测试,是你可能的解决方案的草稿,对吧。

  def p = new DefaultGrailsDomainClass(PersonCommand.class)
  for(PersonCommand person in people) {
    def prop = person.properties.entrySet().findAll{ it.key in p.persistantProperties } 
    if(areAllMapValuesNull(prop)) {
        person.validate()
    } else {}

干杯。

于 2016-05-24T18:13:23.893 回答