Search code examples
admin-on-rest

How to exclude fields from SimpleForm


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!


Solution

  • 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.