check method

FormValidationResponseBag check(
  1. dynamic validate(
    1. List<FormValidator>
    ), {
  2. dynamic onSuccess()?,
  3. dynamic onValidationError(
    1. FormValidationResponseBag
    )?,
})

Check data against validation rules from the controller.

Works the same as check() in a page: the callback receives a list of validators, .that() adds one for a value and returns it so rules can be chained, and every validator is checked once the callback returns. The results come back as a FormValidationResponseBag.

check((validate) {
  validate.that(email.text, label: "Email").email();
  validate.that(password.text, label: "Password").notEmpty();
}, onSuccess: () {
  submit();
}, onValidationError: (bag) {
  showToastDanger(description: bag.firstErrorMessage ?? '');
});

Unlike validate, this runs here rather than in the page, so it works without a state and the controller keeps the result.

Implementation

FormValidationResponseBag check(
  Function(List<FormValidator>) validate, {
  Function()? onSuccess,
  Function(FormValidationResponseBag)? onValidationError,
}) {
  List<FormValidator> allValidators = [];
  validate(allValidators);

  FormValidationResponseBag bag = [
    for (FormValidator validator in allValidators) validator.check(),
  ];

  if (onValidationError != null && !bag.isValid) {
    onValidationError(bag);
  }
  if (onSuccess != null && bag.isValid) {
    onSuccess();
  }
  return bag;
}