Fix: useInput
should call a custom validator with the final source in respect to the <SourceContext>
#10148
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
When passing a custom validator function to an input, the
source
prop it recieves is the originalsource
prop declared on the input, instead of the 'final' source computed using the<SourceContext>
.This is an issue when the input is inside a component wrapping it with a custom
<SourceContext>
, such as<SimpleFormIterator>
.Example:
In that case,
validator
will be called like so:validator("value", { posts: [{ title: "value" }] }, { source: "title", /* ... */ })
Although it should have been:
validator("value", { posts: [{ title: "value" }] }, { source: "posts.0.title", /* ... */ })
Solution
Use the
<SourceContext>
and pass a prop calledfinalSource
when calling the custom validator in addition to the originalsource
prop.validator("value", { posts: [{ title: "value" }] }, { source: "title", finalSource: "posts.0.title", /* ... */ })
How To Test
Run unit tests.
Alternatively, use a custom validator inside an
<ArrayInput>
in one of the demos.Additional Checks
master
for a bugfix, ornext
for a featureAlso, please make sure to read the contributing guidelines.