简体   繁体   English

验证 grails 域 object 中的一组字段

[英]Validating a group of fields in grails domain object

I have a command object that captures a feedback form with 3 textareas.我有一个命令 object 捕获带有 3 个文本区域的反馈表。

class FeedbackCommand {
    String textarea1
    String textarea2
    String textarea3
    String username 

    static constraints = {
        textarea1(nullable:true, blank:true)            
        textarea2(nullable:true, blank:true)            
        textarea3(nullable:true, blank:true)            
        username(nullable:false, blank:false)
    }
}    

I'd like to ensure that at least ONE of the textareas is filled out.我想确保至少填写一个文本区域。

I came up with adding a fake flag field as a 'constraint' field, and then doing a bunch of object checks in the custom validator for that field.我想出了添加一个假标志字段作为“约束”字段,然后在该字段的自定义验证器中执行一堆 object 检查。 If after looking around in myself and i dont find what I want, I throw an error.如果在自己环顾四周后找不到我想要的东西,我会抛出一个错误。

Right now, I'm doing this:现在,我正在这样做:

class FeedbackCommand {
    String textarea1
    String textarea2
    String textarea3
    boolean atLeastOne = true
    String username 

    static constraints = {
        textarea1(nullable:true, blank:true)            
        textarea2(nullable:true, blank:true)            
        textarea3(nullable:true, blank:true) 
        atLeastOne(validator: { boolean b, FeedbackCommand form, Errors err ->
          if (b) {
            if ( (form.textarea1==null || form.textarea1?.isAllWhitespace()) &&
                 (form.textarea2==null || form.textarea2?.isAllWhitespace()) &&
                 (form.textarea3==null || form.textarea3?.isAllWhitespace()))
            {
                // They havent provided ANY feedback. Throw an error
                err.rejectValue("atLeastOne", "no.feedback")
                return false
            }
          }
          return true             
        })           
        username(nullable:false, blank:false)
    }
}

Is there a better way to有没有更好的方法

  • validate a related/group of fields (at least one can't be blank, 2 should have values, etc)?验证相关/一组字段(至少一个不能为空,2 个应该有值等)?
  • a groovier way to express "at least one shouldnt be null/blank" rather than my gross if-statement block?表达“至少一个不应该为空/空白”而不是我的总 if 语句块的更时髦的方式?

Thanks谢谢

The Extended Validation plugin also adds support for instance validators, which allow to define constraints over several field without defining an artificial flag field or without repeating the validator for each field involved. Extended Validation插件还添加了对实例验证器的支持,它允许在多个字段上定义约束,而无需定义人工标志字段或为每个涉及的字段重复验证器。

validate a related/group of fields (at least one can't be blank, 2 should have values, etc)?验证相关/一组字段(至少一个不能为空,2 个应该有值等)?

Try this:尝试这个:

if ( (form.textarea1?.trim() ? 1 : 0) +
     (form.textarea2?.trim() ? 1 : 0) +
     (form.textarea3?.trim() ? 1 : 0) < 2) {
     err.rejectValue("atLeastTwo", "no.feedback")
     return false
}

a groovier way to express "at least one shouldnt be null/blank" rather than my gross if-statement block?表达“至少一个不应该为空/空白”而不是我的总 if 语句块的更时髦的方式?

This is slightly Groovier...这有点 Groovier ......

if (!( (form.textarea1?.trim() ?: 0) ||
     (form.textarea2?.trim() ?: 0) ||
     (form.textarea3?.trim() ?: 0) )) {
     err.rejectValue("atLeastOne", "no.feedback")
     return false
}

WRT validating a group of fields, you could assign the validator closure to one of the fields. WRT 验证一组字段,您可以将验证器闭包分配给其中一个字段。 You don't need any extra/ficticious field.您不需要任何额外/虚构的领域。 If it's going to be used often, create a plugin http://www.zorched.net/2008/01/25/build-a-custom-validator-in-grails-with-a-plugin/ or use a plugin for constraints http://grails.org/plugin/constraints如果要经常使用,请创建一个插件http://www.zorched.net/2008/01/25/build-a-custom-validator-in-grails-with-a-plugin/或使用插件约束http://grails.org/plugin/constraints

About grooviness I'm not an expert.关于 grooviness 我不是专家。 But the safe navigator operator ?.但是安全导航操作员 makes unnecessary to ask for null无需询问 null

if ( form.textarea1?.isAllWhitespace() &&
     form.textarea2?.isAllWhitespace() &&
     form.textarea3?.isAllWhitespace() )
     {
          // They havent provided ANY feedback. Throw an error
          err.rejectValue("atLeastOne", "no.feedback")
          return false
     }

You can use the min-criteria plugin for that.您可以为此使用 min-criteria 插件。 http://www.grails.org/plugin/min-criteria http://www.grails.org/plugin/min-criteria

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

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