Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 导入的唯一约束不';没有得到验证_Spring_Grails_Constraints - Fatal编程技术网

Spring 导入的唯一约束不';没有得到验证

Spring 导入的唯一约束不';没有得到验证,spring,grails,constraints,Spring,Grails,Constraints,我有一个SpringSecurity用户类,它对用户名和电子邮件有唯一的约束。在命令类中,我使用“importFrom User”从该类导入了所有约束。除唯一约束外,所有约束均按预期工作 但是,保存用户时,将验证唯一约束并显示错误。但如果它们像所有其他约束一样在保存之前得到验证,那就太好了 更新 我将此添加到控制器: user.errors.fieldErrors.each { command.errors.rejectValue(it.getField(), it.getCode())

我有一个SpringSecurity用户类,它对用户名和电子邮件有唯一的约束。在命令类中,我使用“importFrom User”从该类导入了所有约束。除唯一约束外,所有约束均按预期工作

但是,保存用户时,将验证唯一约束并显示错误。但如果它们像所有其他约束一样在保存之前得到验证,那就太好了

更新

我将此添加到控制器:

user.errors.fieldErrors.each {
    command.errors.rejectValue(it.getField(), it.getCode())
}

看起来是一个肮脏的解决办法,但它可以工作。

我以前在unique constraint方面遇到过问题,所以我在命令对象中创建了一个自定义验证器来测试它是否唯一:

命令对象:

class wateverCommand{
    ....
    String username

    static constraints = {
        username validator:{value, command ->
            if(value){
                 if(User.findByUsername(value){
                     return 'wateverCommand.username.unique'
                 }
            }
        }
    }
}
在messages.properties中添加自定义错误消息:

wateverCommand.username.unique The username is taken, please pick a new username

我以前遇到过unique约束的问题,所以我在命令对象中创建了一个自定义验证器来测试它是否唯一:

命令对象:

class wateverCommand{
    ....
    String username

    static constraints = {
        username validator:{value, command ->
            if(value){
                 if(User.findByUsername(value){
                     return 'wateverCommand.username.unique'
                 }
            }
        }
    }
}
在messages.properties中添加自定义错误消息:

wateverCommand.username.unique The username is taken, please pick a new username

我同意独特约束似乎并不总是正确导入。因为我喜欢避免约束体中的混乱,所以我喜欢一行方法:

validator: {value, command ->  (User.findByUsername(value) ? false : true ) }
然后在message.properties中:

accountCommand.username.validator.error=That username already exists

我同意独特约束似乎并不总是正确导入。因为我喜欢避免约束体中的混乱,所以我喜欢一行方法:

validator: {value, command ->  (User.findByUsername(value) ? false : true ) }
然后在message.properties中:

accountCommand.username.validator.error=That username already exists

好问题@Chris,您的解决方案是最好的,因为域类和命令对象之间共享约束的目标是避免重复验证逻辑

为了避免重复字段错误和处理域对象中的嵌套字段路径,我将添加如下内容

def save(EntityCreateCommand cmd) {
    def entity = new Entity(cmd.properties)
    def someAssociation = new Something(cmd.properties)
    entity.someAssociation = someAssociation

    entity.validate()

    entity.errors.fieldErrors.each {
        def fieldName = it.field.split("\\.").last()
        def flattenedCodes = cmd.errors.getFieldErrors(fieldName).codes.flatten()
        if(cmd.hasProperty(fieldName) && (!flattenedCodes.contains(it.code))) {
            cmd.errors.rejectValue(fieldName, 
                "entityCreateCommand.${fieldName}.${it.code}")
        }
    }

    if(cmd.errors.hasErrors()) {
        error handling stuff...
    } else {
        business stuff...
    }
}

好问题@Chris,您的解决方案是最好的,因为域类和命令对象之间共享约束的目标是避免重复验证逻辑

为了避免重复字段错误和处理域对象中的嵌套字段路径,我将添加如下内容

def save(EntityCreateCommand cmd) {
    def entity = new Entity(cmd.properties)
    def someAssociation = new Something(cmd.properties)
    entity.someAssociation = someAssociation

    entity.validate()

    entity.errors.fieldErrors.each {
        def fieldName = it.field.split("\\.").last()
        def flattenedCodes = cmd.errors.getFieldErrors(fieldName).codes.flatten()
        if(cmd.hasProperty(fieldName) && (!flattenedCodes.contains(it.code))) {
            cmd.errors.rejectValue(fieldName, 
                "entityCreateCommand.${fieldName}.${it.code}")
        }
    }

    if(cmd.errors.hasErrors()) {
        error handling stuff...
    } else {
        business stuff...
    }
}