简体   繁体   English

如何用grails命令对象编写日期验证器?

[英]How to write a date validator with grails command object?

I want to add a date expression validator in my command object, but I'm not sure what's the correct syntax... 我想在命令对象中添加一个日期表达式验证器,但我不确定正确的语法是什么......

class UserController {
    …
}
class DateServiceCommand {
    String date    //valid format is DD-MMM-YYYY, 01-APR-2011
    static constraints = {
        date(blank:false, ??? )
    }
}

You can use a custom validator : 您可以使用自定义验证器

import java.text.*

class DateServiceCommand {
    String date
    static constraints = {
        date blank: false, validator: { v ->
            def df = new SimpleDateFormat('dd-MMM-yyyy')
            df.lenient = false

            // parse will return null if date was unparseable
            return df.parse(v, new ParsePosition(0)) ? true : false
        }
    }
}

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

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