views:

203

answers:

1

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

e.g.

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

A: 

You can use what the email constraint uses:

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 
            }
        }
    }
}
Burt Beckwith
Thanks - that worked great!
Jason