Validation messages
Last updated
Last updated
Spec: Validation messages
There is no point of validation if it is not properly reflected in the UI to help the user pass it fast and pleasantly.
In this part we are going to focus on how to declare the validation messages, and on the built-in functionality which helps us to craft the most advanced logic using one-line functions.
To reduce the duplication, messages have a list of keywords which correspond to the pre-defined validation statuses of the field.
Similar to the Validation rules, validation messages are based on field selectors. We can select the fields based on type
and name
. However, there is also an additional general
selector, which acts like a fallback for the rules without a specific message.
Declaring a general
messages is a good place to start.
The same keywords can be applied to any selector:
Note that
missing
andinvalid
keywords are optional. In case the respective keyword is not found for the selector, it will use the closest keyword with the same type. Read more about the Fallback system.
To bind a certain message to a rule we need to list it under the rule
keyword of the field selector.
Let's apply some custom messages to multiple validation rules of [type="password"]
field we declared earlier:
Rule-specific messages have the following format:
Each message accepts a string or a function as its value.
This is how we can use a function to resolve the validation message:
With a smart fallback system we can be sure a proper message is always displayed to notify the user of the validation status. Fallback system returns the closest message matching the keyword, when there is no specific message found.
Take a look at the example below.
Notice that messages.type.password
selector doesn't have a missing
message. That doesn't mean that the invalid state of the password will not be reflected in the UI. Instead, React Advanced Form will automatically grab the closest message of the same type - which is messages.general.missing
in this case.
Read more on the Fallbacks in the respective section of the documentation.
Keyword
Description
missing
Applied to empty required field.
invalid
Applied to the field with unexpected value.
async
Applied in case async validation of the field fails.
rule
Collection of custom messages mapped to Named rules.