sunny-green
sunny-green12mo ago

Can't I use edit without entering id in useForm?

export const EditFirstEventProps = ({items}: {items:FirstEventValues }) => {
const [itemsState, setItemsState] = useState<FirstEventValues>(items);
const {
handleSubmit,
refineCore: {onFinish},
} = useForm<HttpError>({
shouldUseNativeValidation: true,
refineCoreProps: {
resource: "firstevent",
action: "edit",
},

});
export const EditFirstEventProps = ({items}: {items:FirstEventValues }) => {
const [itemsState, setItemsState] = useState<FirstEventValues>(items);
const {
handleSubmit,
refineCore: {onFinish},
} = useForm<HttpError>({
shouldUseNativeValidation: true,
refineCoreProps: {
resource: "firstevent",
action: "edit",
},

});
[useForm]: action: "edit", resource: "firstevent", id: undefined If you don't use the setId method to set the id, you should pass the id prop to useForm. Otherwise, useForm will not be able to infer the id from the current URL. ___ const firstEventResponse = await fetch(http://localhost:4000/firstevent); const firstEvent = await firstEventResponse.json(); "first event": { "id": 1, "event term": 1111, "event check": "N" }, We are receiving this type of data. I don't want it to be http://localhost:4000/firstevent/1. How to save data without using id?
2 Replies
kapa.ai
kapa.ai12mo ago
Hi @florent_26629. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- In the useForm hook of Refine, the id is required when the action is set to "edit" or "clone". By default, useForm infers the id from the URL. If the URL doesn't contain the id params, you must provide it manually. This is because the id is used to determine the record to edit or clone. Here is an example of how you can define the id in the useForm options:
const formResult = useForm({
resource: "users",
id: "123456",
});
const formResult = useForm({
resource: "users",
id: "123456",
});
You can also use the setId method to set the id after the form is mounted:
const formResult = useForm({
resource: "users",
});

formResult.setId("123456");
const formResult = useForm({
resource: "users",
});

formResult.setId("123456");
If you want to save data without using an id, you might want to consider using the "create" action instead of "edit". The "create" action is used to create a new record, and it doesn't require an id. Here is an example:
const {
handleSubmit,
refineCore: {onFinish},
} = useForm<HttpError>({
shouldUseNativeValidation: true,
refineCoreProps: {
resource: "firstevent",
action: "create",
},
});
const {
handleSubmit,
refineCore: {onFinish},
} = useForm<HttpError>({
shouldUseNativeValidation: true,
refineCoreProps: {
resource: "firstevent",
action: "create",
},
});
Please note that this will create a new record instead of editing an existing one. For more information, you can refer to the official documentation of Refine here (https://refine.dev/docs/packages/react-hook-form/use-form#id) and here (https://refine.dev/docs/guides-concepts/routing#useform-). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai12mo ago
<@1184365192482652173> kapa.ai is still learning and improving, please let us know how it did by reacting below