-
Notifications
You must be signed in to change notification settings - Fork 85
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
fields from useForm
carries old errors after successful submission + redirect
#396
Comments
This is because Remix does not unmount the page and re-initialize the page if you are redirecting to the same URL and Conform has no idea of the action result. You can either follow the remix approach as explained here or simply return the submission object with payload overrided to null: export async function action({ request }) {
// ...
return json({
...submission,
payload: null
});
} |
Hmm, it's not that I'd like to clear the form inputs, but to clear the error state in the form. I tried your suggestion: export async function action({ request }) {
if (error) {
return json({ ...submission, payload: null })
}
} and upon error, it reverted to the pre-edit value but it didn't show the error state at all. Also, I'm taking the progressive enhancement approach, so I only use Conform in routes that requires nested object access. In other places I still use plain old Zod schema, and this code works pretty well. import { redirectWithSuccess } from 'remix-toast'
export async function action({ request, params }) {
const userId = await requireUserId(request)
const formData = await request.formData()
const payload = Object.fromEntries(formData)
const result = UserSchema.safeParse(payload)
if (!result.success) {
return { errors: result.error.flatten().fieldErrors }
}
await User.update(userId, result.data)
return redirectWithSuccess('', 'User updated!')
} So I hope there's a way to do the same using Conform. |
I would love to have this supported out of the box too! But I just don't have a viable solution at the moment. The issue here is that Conform has no idea about what happened outside of the form event. The only way it reacts to external event is through
My current suggestion is to not doing a redirection in this case and just return the submission with payload function Exmaple() {
const isRedirect = /* ... */
useEffect(() => {
if (isRedirect) {
formRef.current?.reset();
}
}, [isRedirect]);
// ...
} |
Friend pointed me in the right direction and found a solution with https://twitter.com/techtalkjp/status/1752886169912901679 So I removed import { jsonWithError, redirectWithSuccess } from 'remix-toast'
export async function action({ request, params }) {
const submission = parseWithZod(await request.formData(), { schema })
if (submission.status !== 'success') {
return jsonWithError(submission.reply(), 'Failed to update')
}
await User.update(userId, submission.value)
return redirectWithSuccess('', 'User updated!')
} The component / client side looks exactly the same as pre-Conform implementation (just print Also, both I would appreciate if you could consider updating the tutorial accordingly — a lot of users from Rails / Laravel would first expect Post/Get/Redirect pattern, and this will be the smallest first step to work with Conform. |
Using an empty object fallback seems to be working for me: export async function action({ request }) {
const userId = await requireUserId(request)
const formData = await request.formData()
const submission = parse(formData, { schema })
if (submission.intent !== 'submit' || !submission.value) {
return jsonWithError(submission, 'Failed to update')
}
await User.update(userId, submission.value)
return redirectWithSuccess('', 'Updated!')
}
+ const emptyResult = {};
+
export default function Page() {
const { user } = useLoaderData()
const lastSubmission = useActionData()
- const [form, fields] = useForm({ defaultValue: user, lastSubmission })
+ const [form, fields] = useForm({ defaultValue: user ?? emptyResult, lastSubmission })
return <>
...
<p>{fields.name.error}</p>
</>
} The object object has to be created at a module level otherwise React keeps trying to re-render the component. |
Describe the bug and the expected behavior
With a typical server-side validation in Post/Redirect/Get pattern, but I'm getting the stale error message after redirectWithSuccess (to the same URL with GET). It's persistent until I run full page reload with cmd+R.
Conform version
v0.9.1
Steps to Reproduce the Bug or Issue
What browsers are you seeing the problem on?
Chrome
Screenshots or Videos
No response
Additional context
No response
The text was updated successfully, but these errors were encountered: