rival-black
rival-black2d ago

useForm redirect

I want to customise the useForm redirect, I need it to use slug instead of id, can you please show me how to do that my current useForm
const {
saveButtonProps,
refineCore: { formLoading, queryResult, onFinish },
register,
control,
formState: { errors },
setValue,
getValues
} = useForm({
refineCoreProps: {
meta: {
extend_url: 'edit',
},
action: "edit",
redirect: "show",
queryOptions: {
enabled: true,
enabled: !!canAccess?.can,
},
},
});
const {
saveButtonProps,
refineCore: { formLoading, queryResult, onFinish },
register,
control,
formState: { errors },
setValue,
getValues
} = useForm({
refineCoreProps: {
meta: {
extend_url: 'edit',
},
action: "edit",
redirect: "show",
queryOptions: {
enabled: true,
enabled: !!canAccess?.can,
},
},
});
17 Replies
kapa.ai
kapa.ai2d ago
Hi @koop_g. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- To customize the useForm redirect to use a slug instead of an ID, you can modify the redirect function in the refineCoreProps. Here's how you can achieve this:
const {
saveButtonProps,
refineCore: { formLoading, queryResult, onFinish, redirect },
register,
control,
formState: { errors },
setValue,
getValues
} = useForm({
refineCoreProps: {
meta: {
extend_url: 'edit',
},
action: "edit",
redirect: (resource, id, data) => {
// Assuming the slug is available in the form data or the API response
const slug = data?.slug || getValues().slug;
return [`/${resource}/show`, { slug }];
},
queryOptions: {
enabled: !!canAccess?.can,
},
},
});
const {
saveButtonProps,
refineCore: { formLoading, queryResult, onFinish, redirect },
register,
control,
formState: { errors },
setValue,
getValues
} = useForm({
refineCoreProps: {
meta: {
extend_url: 'edit',
},
action: "edit",
redirect: (resource, id, data) => {
// Assuming the slug is available in the form data or the API response
const slug = data?.slug || getValues().slug;
return [`/${resource}/show`, { slug }];
},
queryOptions: {
enabled: !!canAccess?.can,
},
},
});
In this modified version, we've changed the redirect property to a function. This function receives three parameters: 1. resource: The name of the resource 2. id: The ID of the record (which we won't use in this case) 3. data: The data returned from the API after the form submission The function returns an array where the first element is the path, and the second element is an object with query parameters. Here's what the function does: 1. It attempts to get the slug from either the API response data (data?.slug) or from the current form values (getValues().slug). 2. It then returns an array with the show page path and the slug as a query parameter. This approach assumes that you have set up your routes to handle slug-based URLs for the show page. You might need to adjust your routing configuration accordingly. Remember to handle cases where the slug might not be available. You could fall back to using the ID in such cases, or handle the error appropriately. For more information on customizing redirects in refine, you can refer to the Routing documentation. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai2d ago
<@1327562833537400895> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
rival-black
rival-blackOP2d ago
can you point me a documentaion describing your suggestion which is not cover in the https://discord.com/channels/837692625737613362/1343938919187611792/1343938984551911474 link you have provided ? @kapa.ai can you point me a documentaion describing your suggestion which is not cover in the ⁠ask-any-question⁠useForm redirect link you have provided ?
kapa.ai
kapa.ai2d ago
Let me check the knowledge base for you again. One moment! :books: ---------------------- I apologize for the confusion in my previous response. After carefully reviewing the knowledge sources provided, I realize that there isn't a specific documentation that directly describes the custom redirect function I suggested earlier. The information I provided was not entirely accurate based on the available knowledge sources. The most relevant information about redirects in useForm is found in the Refine Core useForm documentation. However, this documentation doesn't cover the specific case of using a slug for redirection instead of an ID. The documentation states that you can use the redirect function to programmatically specify the destination after a successful mutation, but it doesn't provide examples of using custom parameters like slugs. Here's what the documentation actually says about redirects: "By default, after a successful mutation, useForm will redirect to the "list" page. To redirect to a different page, you can either use the redirect function to programmatically specify the destination, or set the redirect property in the hook's options." I apologize that I don't have more specific information about using slugs for redirection in useForm. If you need this specific functionality, you might need to experiment with the existing redirect function or consider reaching out to the Refine community or support channels for more tailored advice. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai2d ago
<@1327562833537400895> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
rival-black
rival-blackOP2d ago
@kapa.ai can BaseRecord help in anyway ?
kapa.ai
kapa.ai2d ago
Let me check the knowledge base for you again. One moment! :books: ---------------------- To answer your question about whether BaseRecord can help with using a slug for redirection in useForm, I'll need to provide some context based on the available knowledge sources. BaseRecord is a type used in Refine to represent the basic structure of a record. It's mentioned in several type definitions in the Refine documentation. However, the knowledge sources don't provide specific information on how BaseRecord could be directly used to solve the slug redirection issue. That being said, BaseRecord might be indirectly helpful in this context. Here's how: 1. BaseRecord is a generic type that extends {id?: string}. This means it can include an id field, but it can also include other fields. 2. In the context of useForm, the TData type parameter (which represents the shape of your data) extends BaseRecord. This means your form data can include any additional fields you need, potentially including a slug. Here's an example of how you might use this information:
interface MyRecord extends BaseRecord {
slug: string;
// other fields...
}

const {
// ...other useForm properties
refineCore: { onFinish, redirect },
} = useForm<MyRecord>({
refineCoreProps: {
redirect: (resource, _id, data) => {
// Assuming data is of type MyRecord
return [`/${resource}/show`, { slug: data.slug }];
},
// ...other props
},
});
interface MyRecord extends BaseRecord {
slug: string;
// other fields...
}

const {
// ...other useForm properties
refineCore: { onFinish, redirect },
} = useForm<MyRecord>({
refineCoreProps: {
redirect: (resource, _id, data) => {
// Assuming data is of type MyRecord
return [`/${resource}/show`, { slug: data.slug }];
},
// ...other props
},
});
In this example, we're extending BaseRecord to include a slug field. Then, in the redirect function, we can access data.slug knowing that it exists on our record type. However, it's important to note that this usage is not explicitly documented in the provided knowledge sources. It's an interpretation based on the available type information and general TypeScript practices. For the most accurate and up-to-date information on using custom fields like slugs in Refine's useForm hook, I recommend checking the latest Refine documentation or reaching out to the Refine community forums. Refine Core useForm documentation provides the basic information about redirects, but doesn't specifically cover using slugs or BaseRecord in this context. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai2d ago
<@1327562833537400895> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
rival-black
rival-blackOP2d ago
@kapa.ai I have added a console.log statment to see if we are going in to this function action: "edit", redirect: (resource, _id, data) => { // Assuming data is of type MyRecord console.log(resource) return [/${resource}/show, { slug: data.slug }]; }, when saving the form I don't see any log prints
kapa.ai
kapa.ai2d ago
Let me check the knowledge base for you again. One moment! :books: ---------------------- Based on the information you've provided and the knowledge sources available, it seems that the redirect function you've added is not being executed as expected. This could be happening for a few reasons, but let's first clarify what the documentation says about the redirect property in useForm. According to the Refine Core useForm documentation, the redirect property can be set to "show" | "edit" | "list" | "create" or false to prevent redirection after form submission. The documentation doesn't explicitly mention using a function for the redirect property. Given that you're not seeing any console logs, it's possible that the function-based approach for redirect is not supported or is not working as intended in your current version of Refine. To troubleshoot this issue, you could try the following: 1. Check if the form submission is successful. The redirect should only happen after a successful mutation. 2. Instead of using a function for redirect, try using a string value as mentioned in the documentation:
refineCoreProps: {
action: "edit",
redirect: "show",
// other props...
}

refineCoreProps: {
action: "edit",
redirect: "show",
// other props...
}

3. If you need custom redirection logic, you might need to handle it manually using the onMutationSuccess callback. Here's an example:
const {
refineCore: { onFinish },
} = useForm({
refineCoreProps: {
action: "edit",
redirect: false, // Disable automatic redirect
onMutationSuccess: (data, variables, context) => {
console.log("Mutation successful", data);
// Your custom redirection logic here
// You can use the `show` function from useNavigation hook
show("your-resource", data.slug);
},
},
});

const {
refineCore: { onFinish },
} = useForm({
refineCoreProps: {
action: "edit",
redirect: false, // Disable automatic redirect
onMutationSuccess: (data, variables, context) => {
console.log("Mutation successful", data);
// Your custom redirection logic here
// You can use the `show` function from useNavigation hook
show("your-resource", data.slug);
},
},
});

Remember to import and use the useNavigation hook to get the show function if you're using this approach. If none of these approaches work, it might be worth checking your Refine version and consulting the most recent documentation or the Refine community forums for the most up-to-date information on custom redirects in useForm. Refine Core useForm documentation ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai2d ago
<@1327562833537400895> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
rival-black
rival-blackOP2d ago
@kapa.ai this worked can you please provide me the documentation page for 'onMutationSuccess'
kapa.ai
kapa.ai2d ago
Let me check the knowledge base for you again. One moment! :books: ---------------------- I'm glad to hear that the approach using onMutationSuccess worked for you. Let me provide you with the relevant documentation for onMutationSuccess in the context of useForm. The onMutationSuccess callback is documented in several places depending on the specific UI integration you're using. Here are the relevant documentation pages: 1. For the core Refine useForm hook: Refine Core useForm documentation 2. For Ant Design integration: Ant Design useForm documentation 3. For Mantine integration: Mantine useForm documentation 4. For React Hook Form integration: React Hook Form useForm documentation These documentation pages all describe the onMutationSuccess callback similarly. It's a function that will be called after a successful mutation, receiving the following parameters: - data: The returned value from useCreate or useUpdate depending on the action. - variables: The variables passed to the mutation. - context: The react-query context. - isAutoSave: A boolean indicating whether the mutation was triggered by the autoSave feature or not. Here's an example of how it's typically used:
useForm({
refineCoreProps: {
onMutationSuccess: (data, variables, context, isAutoSave) => {
console.log({ data, variables, context, isAutoSave });
// Your custom logic here
},
},
});
useForm({
refineCoreProps: {
onMutationSuccess: (data, variables, context, isAutoSave) => {
console.log({ data, variables, context, isAutoSave });
// Your custom logic here
},
},
});
This callback gives you the flexibility to implement custom logic after a successful form submission, including custom redirection as you've done in your case. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai2d ago
<@1327562833537400895> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
rival-black
rival-blackOP2d ago
@kapa.ai can you explain what exactly does it mean by mutation ? does it mean edit/update/patch/put ?
kapa.ai
kapa.ai2d ago
Let me check the knowledge base for you again. One moment! :books: ---------------------- In the context of Refine and the useForm hook, "mutation" refers to any operation that modifies data on the server. This includes creating, updating, and deleting records. So, your understanding is partially correct, but it's a bit broader than just edit/update operations. According to the Refine Core useForm documentation, mutations in useForm can be of different types depending on the action property: 1. For action: "create" or action: "clone", the mutation uses the useCreate hook internally. This typically corresponds to a POST request in REST APIs. 2. For action: "edit", the mutation uses the useUpdate hook. This usually corresponds to PUT, PATCH, or POST requests in REST APIs, depending on how your API is set up. Here's a relevant quote from the documentation: "When in 'create' or 'clone' mode, useForm will call useCreate. When in 'edit' mode, it will call useUpdate and set the resulting values as the mutation property." So, to answer your question directly: - A mutation does include edit/update operations (which could be PUT or PATCH requests). - It also includes create operations (typically POST requests). - While not mentioned in the context of useForm, mutations in general can also include delete operations. The term "mutation" is used in contrast to "query" operations, which only read data without modifying it. Mutations are operations that change the state of data on the server. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai2d ago
<@1327562833537400895> Kapa.ai is still learning and improving, please let me know how I did by reacting below.

Did you find this page helpful?