addErrorMessage method
Add a custom error message to a form attribute's existing error messages.
The addErrorMessage function is used to add a custom error message to the
existing error messages associated with a specific form attribute. It first
checks if the attribute exists in the _attributes map. If the attribute exists,
it retrieves the current error messages, adds the new message to the list, and
updates the error messages for the attribute using the setErrorMessages function.
Additionally, it triggers revalidation for the attribute by calling validate()
on its associated form key and updates the error notifier with the updated
error messages.
Parameters:
attribute: The identifier of the form attribute for which you want to add the custom error message.message: The custom error message to be added to the form attribute's existing error messages.
Implementation
addErrorMessage(String attribute, String message) {
if (!_attributes.containsKey(attribute)) {
return;
}
List<String> messages = getErrorMessages(attribute) ?? [];
messages.add(message);
setErrorMessages(attribute, messages);
getFormKey(attribute).currentState?.validate();
_getErrorNotifier(attribute).value = getErrorMessages(attribute);
}