Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 691
Jason Avatar asked Dec 23 '22 05:12

Jason


1 Answers

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 
            }
        }
    }
}
like image 121
Burt Beckwith Avatar answered May 18 '23 00:05

Burt Beckwith