How to exclude fields from SimpleForm

别来无恙 提交于 2019-12-13 18:57:04

问题


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!


回答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.




回答2:


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 });


来源:https://stackoverflow.com/questions/48600487/how-to-exclude-fields-from-simpleform

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!