Skip to content

Fix ArrayInput root error display on conditional mount#11195

Open
WiXSL wants to merge 9 commits intomasterfrom
arrayinput-onchange-validation
Open

Fix ArrayInput root error display on conditional mount#11195
WiXSL wants to merge 9 commits intomasterfrom
arrayinput-onchange-validation

Conversation

@WiXSL
Copy link
Collaborator

@WiXSL WiXSL commented Mar 19, 2026

Problem

ArrayInput is showing its root-level validation error as soon as it mounted in mode="onChange", which made conditionally rendered arrays appear invalid before any user interaction.

Solution

Extend the subscribed state used by ArrayInput so the root error is only displayed after the field has been interacted with, or after an invalid submit.

How To Test

Open the ConditionalMountOnChangeValidation story in ra-ui-materialui/input/ArrayInput with the fix temporarily removed from ArrayInput.tsx

Additional Checks

  • The PR targets master for a bugfix or a documentation fix, 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)

@WiXSL WiXSL added the RFR Ready For Review label Mar 19, 2026
…ies.tsx

Co-authored-by: Thiery Michel <thiery@marmelab.com>
@WiXSL
Copy link
Collaborator Author

WiXSL commented Mar 20, 2026

I opened a PR in react-hook-form that would improve this fix: react-hook-form/react-hook-form#13319.

It would allow subscribing to submit state directly through subscribe({ formState: ... }), instead of relying on const { isSubmitted } = useFormState().

Update: The PR has been merged
Update 2: A new RHF version with the improvement has been released: v7.72.0

const { subscribe } = useFormContext();

const [error, setError] = React.useState<any>();
const { isSubmitted } = useFormState();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update RH to v.7.2.0 and use the new subscribe({ formState: ... }) ^^

it('should not display a root-level array error immediately when mounted in onChange mode', async () => {
const submit = jest.fn();

const FormWithConditionalArrayInput = () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use the component from the story to avoid duplication

<Create>
<SimpleForm mode="onChange">
<Alert severity="info" sx={{ mb: 2 }}>
Reproduction of bug 2: Displaying the ArrayInput should not
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"bug 2" doesn't mean anything for a Storybook visitor

<Alert severity="info" sx={{ mb: 2 }}>
Reproduction of bug 2: Displaying the ArrayInput should not
automatically mark it as invalid. The error should appear
only after actual interaction or an invalid submission.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I opened this story, I didn't understand what it did or why it existed. You should make it clearer that to trigger validation, one should add then remove an item.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

RFR Ready For Review

Development

Successfully merging this pull request may close these issues.

3 participants