Grails: Validation of string containing a delimited list of email addresses

I have a Grails command object that contains an emailAddresses field,

eg

public class MyCommand {

    // Other fields skipped
    String emailAddresses

    static constraints = {
        // Skipped constraints
    }


}

The user is required to enter a semicolon-delimited list of email addresses into the form. Using Grails' validation framework, what's the easiest way to validate that the string contains a well-formed list of delimited email addresses? Is there any way that I can reuse the existing email address validation constraint?

Thanks


您可以使用电子邮件约束使用的内容:

import org.apache.commons.validator.EmailValidator
...

static constraints = {
    emailAddresses validator: { value, obj, errors ->
        def emailValidator = EmailValidator.getInstance()
        for (email in value.split(';')) {
            if (!emailValidator.isValid(email)) {
                // call errors.rejectValue(), or return false, or return an error code 
            }
        }
    }
}
链接地址: http://www.djcxy.com/p/92930.html

上一篇: 如何发送电子邮件到其中有破折号的地址?

下一篇: Grails:验证包含分隔列表的电子邮件地址的字符串