<FormError />
The <FormError />
component can be 'rendered' to create errors on your Form. The concept of "rendering an Error" might require a small shift in the way you think about form validation. FormError components allow you to tell Yafl what errors are on your form. This has some interesting benefits, one of which is that a "rendered" error solves some of the edge cases around form validation - the most obvious example being that of async validation.
Props
msg?: string
The error message. If this FormError component is rendered with the same path as another FormError component the msg string will the pushed onto an array of error messages for the same path.
path?: Path
Override the path
for a FormError. By default the path
is determined by what appears above this component in the Form component hierarchy.
Why you might need this:
path
This is useful assign errors that belong to the domain of a Section, Repeat, at the Form level. Using the
path
prop is also for simply displaying general errors with a custom path or key.
Example
Here's an example:
caution
Currently Yafl does not guarantee the order in which error messages will appear in a Field's errors
array. However this is usually only important when you only want to display the first error message using something like errors[0]
. Fortunately Yafl provides the syntax that allows you to stop validating Fields "on first failure". You can accomplish this by nesting a <FormError />
as the child of another <FormError />
. This works because the children of a FormError are only rendered when Validation passes for any particular <FormError />
.