开发者

How to write a date validator with grails command object?

开发者 https://www.devze.com 2023-03-30 08:54 出处:网络
I want to add a date expressio开发者_如何转开发n validator in my command object, but I\'m not sure what\'s the correct syntax...

I want to add a date expressio开发者_如何转开发n 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
        }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号