vantezzen / auto-form

🌟 A React component that automatically creates a @shadcn/ui form based on a zod schema.
https://vantezzen.github.io/auto-form/
2.27k stars 81 forks source link

disabling a field in fieldconfig #75

Closed coffeedefi14 closed 4 weeks ago

coffeedefi14 commented 2 months ago

Hi, i am using the following config to disable the date field, but it's not working : fieldConfig={{ date: { inputProps:{ disabled:true}

},

LandSprutte commented 4 weeks ago

Hi,

I had a scenario where I wanted to create an association through my form. In this case I have a Station with one or moreAvailablity-objects, When I create a new Availablity I want the associated StationId in my insert schema.

I found a way to do this by using zods omit . Removing it from the schema, removes it from the form.

e.g:

export const availabilityInsertSchema = z.object({
  title: z.string(),
  from: z.coerce.date(),
  to: z.coerce.date(),
  description: z.string(),
  stationId: z.string(), <--- required, but not a part of the form.
});

export type AvailablityInsert = z.infer<typeof availabilityInsertSchema>;

async function onSubmit(values: Omit<AvailablityInsert, "stationId">) {
    if (!stationId) return;

    const res = await createNewAvailabilityForStation({
      ...values,
      stationId,
    });
   }
}

 <AutoForm
        formSchema={availabilityInsertSchema.omit({
          stationId: true,
        })}
        onSubmit={onSubmit}
       ...
      />
coffeedefi14 commented 4 weeks ago

thanks