Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: useInput should call a custom validator with the final source in respect to the <SourceContext> #10148

Merged
merged 2 commits into from
Aug 19, 2024

Conversation

slax57
Copy link
Contributor

@slax57 slax57 commented Aug 19, 2024

Problem

When passing a custom validator function to an input, the source prop it recieves is the original source 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:

<ArrayInput source="posts">
  <SimpleFormIterator>
    <TextInput source="title" validate={validator} />
  </SimpleFormIterator>
</ArrayInput>

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 called finalSource when calling the custom validator in addition to the original source 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

  • The PR targets master for a bugfix, or next for a feature
  • The PR includes unit tests (if not possible, describe why)
  • The PR includes one or several stories (if not possible, describe why) <- did not find it useful for such a technical case
  • The documentation is up to date

Also, please make sure to read the contributing guidelines.

@slax57 slax57 added the RFR Ready For Review label Aug 19, 2024
@fzaninotto fzaninotto merged commit 8787c4c into master Aug 19, 2024
14 checks passed
@fzaninotto fzaninotto deleted the fix-validator-SourceContext branch August 19, 2024 13:03
@slax57 slax57 added this to the 5.1.2 milestone Aug 19, 2024
slax57 added a commit that referenced this pull request Aug 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
RFR Ready For Review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants