Forms with custom components
Hello! I'm most likely being dumb but I'm currently working on making a create form based off of what was generated by the inferencer. I've made a custom component that I'd like to be a part of the create form:
and here is it implemented in the form:
What should I do to troubleshoot this?
16 Replies
Hi @quandrix_. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
Hmm, I don't know enough to give you a confident answer yet. However, based on the information provided, it seems like you might be having issues with the
useForm
hook and the control
prop.
In refine, useForm
is a hook used to manage forms. It returns the necessary properties and methods to control the form. The control
prop is used to control the form's state and actions. You should ensure that the form instance returned by useForm
is connected to your Form element. If you're using a library like Ant Design, you would typically pass the form
instance as a prop to the Form
component.
Here's a basic usage of the useForm hook from the refine blog:
You can find more information about this in the refine documentation (https://refine.dev/docs).
Also, Refine uses Ant Design's Form component for form management. To ensure that your custom input fields are properly integrated with Refine's form management system, you need to follow these steps:
1. Your custom input fields should be wrapped within the Form.Item
component from Ant Design. This allows the form management system to recognize your custom input fields.
2. Your custom input fields should be controllable components. This means they should accept a value
prop and trigger an onChange
event when the value changes. This allows the form management system to control the value of your custom input fields and listen for changes.
3. If your custom input field uses a different prop name for the value or a different event for changes, you can specify these using the valuePropName
and trigger
props of the Form.Item
component.
You can find more information about this in the refine documentation (https://refine.dev/docs) and refine blog (https://refine.dev/blog).
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@223544018016010240> kapa.ai is still learning and improving, please let us know how it did by reacting below
frail-apricot•10mo ago
@kapa.ai I'm using material UI, not ant
Let me check the knowledge base for you. One moment! :books:
----------------------
In refine, you can use Material UI for your forms and manage them using the
@refinedev/react-hook-form
adapter. This allows you to use all the features of the React Hook Form library in your refine project in a compatible way.
Here's an example of how to use it in a create form from the refine blog:
In your case, you can use the Controller
component from @refinedev/react-hook-form
to wrap your custom AbilityScoreIncrease
component. This will allow you to control the form state and handle validation rules.
For more information, you can refer to the refine documentation (https://refine.dev/docs/examples/form/mui/useForm/) and refine blog (https://refine.dev/blog/build-admin-panel-with-material-ui-and-strapi/).
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@223544018016010240> kapa.ai is still learning and improving, please let us know how it did by reacting below
frail-apricot•10mo ago
@kapa.ai can you give me an example of using a custom component that is passed as a field for the create form?
Let me check the knowledge base for you. One moment! :books:
----------------------
Sure, based on the information provided in the extracts, here's an example of how you can use a custom component in a create form using refine and Material UI:
First, define your custom component. In this case, let's call it
MyCustomComponent
:
Then, in your create form, you can use the Controller
component from @refinedev/react-hook-form
to wrap your custom component. This will allow you to control the form state and handle validation rules:
In this example, MyCustomComponent
is used as a custom input field in the create form. The Controller
component from @refinedev/react-hook-form
is used to control the form state and handle validation rules for the custom input field.
For more information, you can refer to the refine documentation (https://refine.dev/docs/examples/form/mui/useForm/) and refine blog (https://refine.dev/blog/build-admin-panel-with-material-ui-and-strapi/).
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@223544018016010240> kapa.ai is still learning and improving, please let us know how it did by reacting below
frail-apricot•10mo ago
@kapa.ai When I hit the save button in the create component, nothing is happening when I have the custom component added. When I remove the component, the form correctly submits.
Let me check the knowledge base for you. One moment! :books:
----------------------
Based on the information provided in the extracts, it seems like there might be an issue with how your custom component is handling form submission.
In refine, form submission is typically handled by the
handleSubmit
method from the useForm
hook. This method is passed to the onSubmit
event handler of the form. Here's an example from the refine blog:
In this example, handleSubmit
is passed the handleSubmission
function, which is called when the form is submitted.
If your custom component is preventing the form from being submitted, it might be because it's not correctly handling the onChange
event or the value prop. Your custom component should accept a value
prop and trigger an onChange
event when the value changes. This allows the form management system to control the value of your custom input fields and listen for changes.
Here's an example of a custom component that correctly handles the onChange
event and the value prop:
If you're still having issues, it might be helpful to check the console for any error messages or warnings. These could provide more information about what's going wrong.
For more information, you can refer to the refine documentation (https://refine.dev/docs/examples/form/mui/useForm/) and refine blog (https://refine.dev/blog/build-admin-panel-with-material-ui-and-strapi/).
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@223544018016010240> kapa.ai is still learning and improving, please let us know how it did by reacting below
frail-apricot•10mo ago
@kapa.ai This is the component I am using
Let me check the knowledge base for you. One moment! :books:
----------------------
Your
AbilityScoreIncrease
component seems to be correctly set up with the Controller
component from @refinedev/react-hook-form
. However, the issue might be with how you're using this component in your form.
When you use the AbilityScoreIncrease
component in your form, you're setting the name
prop to "modifiers.AbilityScoreIncrease"
. However, inside your AbilityScoreIncrease
component, you're setting the name
prop of each Controller
to the ability
value. This means that the names of your fields are not correctly nested under "modifiers.AbilityScoreIncrease"
.
To fix this, you should concatenate "modifiers.AbilityScoreIncrease"
with the ability
value to correctly nest your fields. Here's how you can do it:
This way, the names of your fields will be "modifiers.AbilityScoreIncrease.strength"
, "modifiers.AbilityScoreIncrease.dexterity"
, etc., which correctly nests them under "modifiers.AbilityScoreIncrease"
.
For more information, you can refer to the refine documentation (https://refine.dev/docs/examples/form/mui/useForm/) and refine blog (https://refine.dev/blog/build-admin-panel-with-material-ui-and-strapi/).
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@223544018016010240> kapa.ai is still learning and improving, please let us know how it did by reacting below
frail-apricot•10mo ago
@kapa.ai I'm now able to submit correctly. However when submitting it is putting the ability scores in the root of the reuqest body instead of inside the modifiers.abilityscoreincrease object