我有一个类:Grails的3 - 检查是否命令对象的值均为空
class PersonCommand implements Validateable {
String firstName
String lastName
static constraints = {
firstName nullable: true
lastName nullable: true
}
}
我有型PersonCommand的名单,我需要验证。我想遍历每个元素并检查firstName和lastName是否都为null。有没有办法做到这一点,而没有明确检查这些属性?我想这样做:
for(PersonCommand person in people) {
if(areAllMapValuesNull(person.properties)) {
person.validate()
} else {
...
但person.properties具有其他属性添加到它不仅仅是firstName和lastName,因为它是Validateable。我没有在areAllMapValuesNull()函数上寻找帮助,只是在没有硬编码检查的情况下获取值firstName和lastName。
尝试'person.properties.entrySet()。findAll {it.key in [“firstName”,“lastName”]}'而不是'person.properties' –
不幸的是,它也有像class,errors,constraints,和constraintsMap。 – Anonymous1
当你在[“firstName”,“lastName”]}中做findAll {it.key之后'或者只对所有持久性属性使用'findAll {it.key in person.persistantProperties.name}'时,它不会包含类,错误约束等属性.. –