validate method

  1. @override
bool validate(
  1. String? value,
  2. Map<String, FormFieldState> fields
)
override

Validates the given value against the validation rule.

  • value: The value to be validated.
  • fields: A map of form field states for cross-field validation.

Returns true if the validation passes, false otherwise.

Implementation

@override
bool validate(String? value, Map<String, FormFieldState> fields) {
  if (value == null || value.isEmpty) {
    return true; // empty validation should be handled by a required rule
  }
  // A basic pattern for phone numbers. This will work for several formats, but it might be
  // necessary to adjust it according to the specific requirements or local standards.
  final pattern =
      r'^(?:\+?\d{1,3}?[-.\s]?)?(?:\(\d{1,3}\)?[-.\s]?)?\d{1,3}[-.\s]?\d{1,4}[-.\s]?\d{1,4}$';
  final regex = RegExp(pattern);

  return regex.hasMatch(value);
}