validateAll method

bool validateAll(
  1. String? validator(
    1. int index
    ), {
  2. bool focusFirstInvalid = true,
})

Validates all items using the provided validator function.

The validator receives the index and should return an error message (or null/empty for valid). Optionally focuses the first invalid item.

Returns true if all items are valid, false otherwise.

Usage:

final allValid = focus.validateAll(
  (index) {
    final value = values[index].text;
    if (value.isEmpty) return 'Required';
    return null; // valid
  },
  focusFirstInvalid: true,
);
if (allValid) submit();

Implementation

bool validateAll(
  String? Function(int index) validator, {
  bool focusFirstInvalid = true,
}) {
  int? firstInvalid;

  for (var i = 0; i < _itemCount; i++) {
    final error = validator(i);
    setError(i, error);
    if (error != null && error.isNotEmpty && firstInvalid == null) {
      firstInvalid = i;
    }
  }

  if (focusFirstInvalid && firstInvalid != null) {
    _focusedIndex = firstInvalid;
  }

  return firstInvalid == null;
}