validate method

  1. @nonVirtual
String? validate(
  1. Map<String, FormFieldState> fields
)

Validates the form field against its validation rules.

  • fields A map of form field states for cross-field validation.

Returns an error message if validation fails, otherwise returns null.

Implementation

@nonVirtual
String? validate(Map<String, FormFieldState> fields) {
  final optionalRule = rules.whereType<Optional>().firstOrNull;
  if (optionalRule != null) {
    if (optionalRule.validate(value, fields)) {
      return null; // No validation error, skip other rules
    }
  }

  // Validate against all rules
  for (final rule in rules) {
    if (!(rule is Optional) && !rule.validate(value, fields)) {
      return rule.message(label); // Return the first error message
    }
  }

  return null; // All validations passed
}