[BUG] Form errors not displaying properly in Modal
NextUI Version
2.6.0
Describe the bug
[BUG] Form errors not displaying properly in Modal
Describe the bug When using a form within a NextUI Modal component, form errors are not displaying correctly. The errors only appear when the modal is closed and reopened, rather than in real-time as expected.
Additional context
- This issue occurs when using libraries for form management (e.g., React Hook Form)
- The behavior differs between real-time usage and when using libraries
- There may be compatibility issues with React 19 and
useActionState
Code Example
import { Modal, Button, Input, useDisclosure } from "@nextui-org/react";
import { useActionState } from "react"; // Note: This is hypothetical, not part of NextUI
// Import server
export default function App() {
const { isOpen, onOpen, onClose } = useDisclosure();
const [{errors}, formAction] = useActionState(createTodo, {
errors: {},
});
return (
<div>
<Button auto onPress={onOpen}>
Open modal
</Button>
<Modal
isOpen={isOpen}
onClose={onClose}
closeButton
aria-labelledby="modal-title"
>
<Modal.Header>
<h2 id="modal-title">Test Form</h2>
</Modal.Header>
<Modal.Body>
<Form action={formAction} validationErrors={errors}>
<Input name="todo" label="Task" />
<Button type="submit">Add</Button>
</Form>
</Modal.Body>
</Modal>
</div>
);
// app/actions.ts
"use server";
export async function createTodo(prevState: any, formData: FormData) {
try {
// Create the todo.
} catch (err) {
return {
errors: {
todo: "Invalid todo.",
},
};
}
}
Additional Information Any additional information, configuration, or context that might be helpful in understanding and reproducing the issue. I apologize if there are any mistakes because I wrote it on the phone.
Your Example Website or App
No response
Steps to Reproduce the Bug or Issue
Steps to Reproduce
- Create a NextUI Modal component
- Add a form with input fields inside the Modal content
- Implement form validation (e.g., using React Hook Form)
- Submit the form with invalid data
- Observe that no errors are displayed
- Close and reopen the Modal
- Observe that errors are now visible
Expected behavior
Expected behavior Form errors should display in real-time within the Modal, without requiring the Modal to be closed and reopened.
Screenshots or Videos
No response
Operating System Version
Windows
Browser
Chrome
@tazissam The NextUI Form component is implemented using the native <form>. The issue here is likely due to <form>'s action causing an automatic form reset event (see facebook/react#29034). When the form resets, the form will clear its own error, but the server error is stored outside the form and persists. Thus, when you re-open the modal, the form gets initialized with the previous error.
There are various proposed solutions in the linked React issue's discussion that you can reference. Depending on your requirements, one way to opt out of this automatic reset is to wrap formAction with startTransition from the useTransition hook and calling it in onSubmit instead.
const [state, formAction] = useActionState(createTodo, null);
const [isPending, startTransition] = useTransition();
const handleSubmit = (e) => {
e.preventDefault();
const formData = new FormData(e.target);
startTransition(() => {
formAction(formData);
});
};
// ...
<Form
validationErrors={state?.errors}
onSubmit={handleSubmit}
>
{/* ... */}
</Form>
Please let me know if this addresses your issue.
Maybe need to change example? Because it does not work. And It works only like this https://github.com/heroui-inc/heroui/issues/4300#issuecomment-2537018475
Server errors don`t displayed