validate method

  1. @override
bool validate({
  1. bool clearCustomError = true,
  2. bool focusOnInvalid = true,
  3. bool autoScrollWhenFocusOnInvalid = false,
})
override

Validate field

Clear custom error if clearCustomError is true. By default true

Focus when field is invalid if focusOnInvalid is true. By default true

Auto scroll when focus invalid if autoScrollWhenFocusOnInvalid is true. By default false.

Note: If a invalid field is from type TextField and will focused, the form will auto scroll to show this invalid field. In this case, the automatic scroll happens because is a behavior inside the framework, not because autoScrollWhenFocusOnInvalid is true.

Implementation

@override
bool validate({
  bool clearCustomError = true,
  bool focusOnInvalid = true,
  bool autoScrollWhenFocusOnInvalid = false,
}) {
  if (clearCustomError) {
    setState(() => _customErrorText = null);
  }
  final isValid = super.validate() && !hasError;

  final fields = _formBuilderState?.fields ??
      <String, FormBuilderFieldState<FormBuilderField<dynamic>, dynamic>>{};

  if (!isValid &&
      focusOnInvalid &&
      (formState?.focusOnInvalid ?? true) &&
      enabled &&
      !fields.values.any((e) => e.effectiveFocusNode.hasFocus)) {
    focus();
    if (autoScrollWhenFocusOnInvalid) ensureScrollableVisibility();
  }

  return isValid;
}