You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I ran into a scenario where saving a record in my db failed (this happened in a remix action). I noticed that the form did not populate with the previous entries that were submitted. Here is an example:
typeActionData=SubmissionResult|undefinedconstRegistrySchema=z.object({title: z.string().min(1,'Title is required'),eventType: z.enum(['birthday','wedding','baby-shower','other'],{errorMap: ()=>({message: 'Please select an event type'}),}),eventDate: z.string().min(1,'Event date is required').refine((str)=>!isNaN(Date.parse(str)),{message: 'Invalid date format',}).transform((str)=>{constdate=newDate(str)// Remove time component for consistencyreturnnewDate(date.getFullYear(),date.getMonth(),date.getDate())}),location: z.string().min(1,'Location is required'),guestCount: z.number().min(1,'Must have at least 1 guest'),description: z.string().optional(),})exportasyncfunctionaction({ request }: ActionFunctionArgs){constformData=awaitrequest.formData()constsubmission=parseWithZod(formData,{schema: RegistrySchema,})if(submission.status!=='success'){returnsubmission}// throw a response with the submission valuetry{thrownewError('FAILED TO SAVE TO DB')constuserId=awaitrequireUserId(request)constregistry=awaitprisma.registry.create({data: {
...submission.value,
userId,},})returnredirect(`/registries/${registry.id}`)}catch(error){console.error('Failed to create registry:',error)console.log('IN THE CATCH BLOCK AFTER THE DB CALL')returnResponse.json({
...submission,status: 'error'asconst,error: {form: 'Failed to create registry. Please try again.'},value: submission.value,payload: formData,},{status: 500},)}}exportdefaultfunctionNewRegistry(){constlastResult=useActionData<typeofaction>()asActionDataconsole.log('lastResult',lastResult)const[form,fields]=useForm({
lastResult,constraint: getZodConstraint(RegistrySchema),onValidate({ formData }){returnparseWithZod(formData,{schema: RegistrySchema})},defaultValue: {title: 'TESTING',eventType: '',eventDate: '',location: '',guestCount: 1,description: '',},shouldValidate: 'onBlur',})return(<divclassName="mx-auto max-w-3xl p-8"><h1className="mb-8 text-2xl font-bold">Create New Registry</h1><Formmethod="post"className="space-y-6"{...getFormProps(form)}><div><LabelhtmlFor={fields.title.id}>Registry Title</Label><Input{...getInputProps(fields.title,{type: 'text',})}/>{/* {fields.title.errors?.length && ( <span className="text-red-500">{fields.title.errors}</span> )} */}</div><div><LabelhtmlFor={fields.eventType.id}>Event Type</Label><select{...getSelectProps(fields.eventType)}className="w-full rounded border border-gray-300 px-3 py-2 text-black"><optionvalue="">Select event type...</option><optionvalue="birthday">Birthday</option><optionvalue="wedding">Wedding</option><optionvalue="baby-shower">Baby Shower</option><optionvalue="other">Other</option></select>{/* {fields.eventType.errors?.length && ( <span className="text-red-500">{fields.eventType.errors}</span> )} */}</div><div><LabelhtmlFor={fields.eventDate.id}>Event Date</Label><Input{...getInputProps(fields.eventDate,{type: 'date'})}/>{/* {fields.eventDate.errors?.length && ( <span className="text-red-500">{fields.eventDate.errors}</span> )} */}</div><div><LabelhtmlFor={fields.location.id}>Location</Label><Input{...getInputProps(fields.location,{type: 'text'})}/>{/* {fields.location.errors?.length && ( <span className="text-red-500">{fields.location.errors}</span> )} */}</div><div><LabelhtmlFor={fields.guestCount.id}>Number of Guests</Label><Input{...getInputProps(fields.guestCount,{type: 'number'})}/>{/* {fields.guestCount.errors?.length && ( <span className="text-red-500">{fields.guestCount.errors}</span> )} */}</div><div><LabelhtmlFor={fields.description.id}>Description</Label><textarea{...getTextareaProps(fields.description)}className="font-blue w-full rounded border border-gray-300 px-3 py-2 text-black"rows={4}/>{/* {fields.description.errors?.length && ( <span className="text-red-500">{fields.description.errors}</span> )} */}</div><Buttontype="submit">Create Registry</Button></Form></div>)}
Why doesn't the form populate with the values from lastResult? When I log lasResult the values are there in lastResult.value.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I ran into a scenario where saving a record in my db failed (this happened in a remix action). I noticed that the form did not populate with the previous entries that were submitted. Here is an example:
Why doesn't the form populate with the values from
lastResult
? When I log lasResult the values are there inlastResult.value
.Beta Was this translation helpful? Give feedback.
All reactions