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
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
}
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With