validate method

  1. @override
String? validate({
  1. required String label,
  2. required String? value,
})
override

Ensures the value is formatted as a valid email address.

This will pass on empty or null values.

See also:

Implementation

@override
String? validate({
  required String label,
  required String? value,
}) {
  String? error;

  if (value?.isNotEmpty == true) {
    // Credit to this SO answer: https://stackoverflow.com/a/16888554
    const pattern =
        r"^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,253}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,253}[a-zA-Z0-9])?)*$";

    final regExp = RegExp(pattern);

    if (!regExp.hasMatch(value ?? '')) {
      error = translate(
        FormValidationTranslations.form_validation_email,
        {
          'label': label,
        },
      );
    }
  }

  return error;
}