setErrorMessage method

dynamic setErrorMessage(
  1. String attribute,
  2. String message
)

Set a custom error message for a form attribute.

The setErrorMessage function is used to set a custom error message for a specific form attribute. It first checks if the attribute exists in the _attributes map. If the attribute exists, it sets the custom error message in the _errors map and triggers revalidation for the attribute by calling validate() on its associated form key. Additionally, it updates the error notifier with the new error message.

Parameters:

  • attribute: The identifier of the form attribute for which you want to set the custom error message.
  • message: The custom error message to be associated with the form attribute.

Implementation

setErrorMessage(String attribute, String message) {
  if (!_attributes.containsKey(attribute)) {
    return;
  }
  _errors[attribute] = [message];

  getFormKey(attribute).currentState?.validate();
  _getErrorNotifier(attribute).value = getErrorMessages(attribute);
}