1

I know <SimpleForm> is using redux-form. I want to exclude specific fields from being sent when I submit the form. When I edit my entity "User" the RestClient send a GET_ONE request and my API response this:

{
name: "Lior",
age: "100",
type: "Manager",
}

The <SimpleForm> is like this:

<SimpleForm> 
  <TextInput source="name"/>
  <TextInput source="age"/>
</SimpleForm>

When I send the form, the fields I see in the request are: name, age and type even when there is no "input" or "field" for type.

How can I avoid from "type" to be sent?

  • The API has to return it, I need it for <Show> and more..
  • I don't to hard coded my RestClient just to remove the "type" because it is not happening just in "User" model but in many other models.
  • I don't want to "usset" it on the API controller side.

Thanks!

llioor
  • 5,804
  • 4
  • 36
  • 44

2 Answers2

1

There's no react-admin way to doing this. I think a restClient middleware is the way to go, but you don't want that. What you can do is to create a HOC using mapProps of recompose and wrap SimpleForm with it. Something like (untested code):

const withLimitedProps = properties => mapProps(({save,...props}) => ({...props,save: (record,redirect) => save(properties.reduce((acc,property)=>{
  acc[property]=record[property]
},{})});

const UserEditForm = withLimitedProps(['name','age'])(SimpleForm)

The save prop is proxied, and the record that's submitted is reduced. You can always add more functionality, like selecting the current fields from the redux-form state and reducing on them only. This would give the behaviour that you want.

Dennie de Lange
  • 2,776
  • 2
  • 18
  • 31
  • Thanks again for your answer! I'll give it a try – llioor Feb 04 '18 at 14:21
  • Hi... I added same question in react-admin: https://stackoverflow.com/questions/55010068/react-admin-exclude-record-from-the-redux-form – llioor Mar 06 '19 at 12:29
1

You can customize your restClient to only send a subset of fields when calling UPDATE:

case UPDATE:
    url = `${apiUrl}/${resource}/${params.id}`;
    options.method = 'PUT';
    const { name, age } = params.data; // type is ignored
    options.body = JSON.stringify({ name, age });
François Zaninotto
  • 7,068
  • 2
  • 35
  • 56
  • Thanks Francois, I mentioned this this solution in my post. Unfortunately it is not good for me because I will need to have a custom case in my restClient for each model in my project (around 20). – llioor Feb 06 '18 at 13:25
  • 1
    You can *decorate* your rest client with another function, which removes the field(s) you don't want from the request payloads. See [Decorating REST Clients](https://marmelab.com/admin-on-rest/RestClients.html#decorating-your-rest-client-example-of-file-upload) in the aor documentation for more details. – François Zaninotto Feb 08 '18 at 07:42