Grails: checking a string containing a list of delimited 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 must enter a comma-delimited list of email addresses in the form. Using the Grails validation system, what is the easiest way to verify if a string contains a strict list of split email addresses? Is there a way so that I can reuse the existing email validation restriction?

thank

+3
source share
1 answer

You can use what uses the email restriction:

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 
            }
        }
    }
}
+10
source

Source: https://habr.com/ru/post/1734933/


All Articles