Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create custom form in react-admin

I am using Edit and SimpleForm from react-admin. How do I create a custom form to allow customised action and type on submit?

App.js

<Resource name="category" list={CategoryList} edit={CategoryEdit}  />

index.js

<Edit actions={<CategoryEditActions />} title={<CategoryTitle />} {...props} >
    <SimpleForm>
      <DisabledInput source="id" />
      <DisabledInput source="code" />
      <TextInput source="name" />
   </SimpleForm>

Here the api call would be /category/:categoryId with PUT request. I want to modify url to /category/:categoryId/test with method as POST. Is there any way to customize this?

I have handled this in my CustomDataProvider -

 case UPDATE:
             if(resource === 'category'){
                    options.method = 'POST';
                    url = `${apiUrl}/${resource}/${params.id}/test`;
                } else {
                    options.method = 'PUT';
                    url = `${apiUrl}/${resource}/${params.id}`;
                }
                break;

Is there any other way to handle it?

like image 489
Chetan Gawai Avatar asked Sep 17 '25 19:09

Chetan Gawai


1 Answers

This is the job of your dataProvider in react-admin (restClient in admin-on-rest). You'll have to create a custom one:

  • For react-admin: https://marmelab.com/react-admin/DataProviders.html#writing-your-own-data-provider
  • For admin-on-rest: https://marmelab.com/admin-on-rest/RestClients.html#writing-your-own-rest-client

You'll have to check the resource and type then build the fetch options by yourself.

like image 176
Gildas Garcia Avatar answered Sep 23 '25 11:09

Gildas Garcia