简体   繁体   English

扩展Grails命令对象约束验证

[英]Extended Grails command object constraints validation

Having the following commandobjects in Grails: 在Grails中具有以下命令对象:

class commandA implements commandObjectType {
    String a

    static constraints = {
        a blank: false
    }
}

and

class commandB extends commandA {
    String b
}

How would one the implement custom field validation on b in the commandB object? 如何在commandB对象中的b上实现自定义字段验证? It is to my knowledge not possible to override or in other ways change a closure.. 据我所知,不可能覆盖或以其他方式更改闭包。

Can this be done in anyway? 仍然可以这样做吗? I have tried "shifting" in a closure, without succes.. Is it possible to specify validators in-line with the fields in any way? 我曾尝试在闭包中“转移”,但没有成功。是否可以通过任何方式指定与字段一致的验证器?

I'm not sure if sharing contraints works for command objects, but you can try something like this: 我不确定共享约束是否适用于命令对象,但是您可以尝试如下操作:

class commandB extends commandA {
    String b

    static constraints = {
        importFrom commandA
    }
}

See the Grails documentation about constraints usage . 有关约束使用的信息,请参见Grails文档。

Just define "constraints" block in commandB as usual with validation rules for "b" field. 只需像往常一样在commandB中定义“约束”块,并使用“ b”字段的验证规则。 Validation should work for both "a" and "b" fields. 验证对“ a”和“ b”字段均适用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM