react-admin 排除“记录"从 redux 形式 [英] react-admin exclude "record" from the redux-form
问题描述
当我使用 react-admin 发送 SimpleForm(编辑)请求时遇到问题.该请求包含的参数多于我在表单字段中的参数.
I have an issue when I send SimpleForm (edit) request with react-admin. The request includes more parameters than I have in the form's fields.
例如我有表格:
<Edit {...props}>
<SimpleForm>
<TextInput source="title_new" />
<TextInput source="age_new" />
</SimpleForm>
</Edit>
它仅包含 2 个字段,但是当我单击保存"时,请求包含更多字段.我知道这些字段来自 GET_ONE 请求,这些请求填充了数据库中的数据.
It includes only 2 fields but when I click "save" the request includes more fields. I understood that those fields are coming from the GET_ONE request which fill the data from the DB.
GET_ONE:
{
title: 'title',
title_new: 'title-new',
age: 'age',
age_new: 'age-new',
}
更新请求UPDATE:
The update request UPDATE:
{
title: 'title',
title_new: 'title-new',
age: 'age',
age_new: 'age-new',
}
我希望 UPDATE 将只包含表单字段(title_new
和 age_new
),而没有 title
和 来自记录"的年龄
字段.
I expect that the UPDATE will include only the forms fields (title_new
and age_new
) without the title
and age
fields that come from "record".
这些字段在 API 方面给我带来了很多麻烦,我想从所有表单中避免/排除它们,基本上我只想发送带有 SimpleForm 输入的表单输入.
Those fields make me a lot of trouble on the API side and I want to avoid/exclude them from all the forms, basically I want to send only the form inputs with the SimpleForm inputs only.
我想到的几个解决方案:1.提交前更改表单值"这里2.在restProvider中操作请求
Few solutions I have in mind: 1. "Altering the Form Values before Submitting" here 2. Manipulate the request in the restProvider
这两种解决方案都不适合我,因为我有很多这样的表单,并且 restProvider 代码看起来很糟糕.此外,我不想改变"我构建的任何表单.
Both solutions are not good for me because I have many forms like that and the restProvider code will look bad. Also I don't want to "alter" any form I build.
请多多指教.
推荐答案
这是 react-admin 的工作方式.如果您希望 UPDATE dataProvider 动词仅发布更改字段(并且可能发送 PATCH 而不是 POST),则必须在 dataProvider 中执行此操作.
This is the way react-admin works. If you want the UPDATE dataProvider verb to post only the changes fields (and probably send a PATCH rather than a POST), you have to do it in the dataProvider.
我不确定更改后提供程序会不会看起来很糟糕:您所要做的就是更改 UPDATE 动词.默认情况下,它看起来像(对于简单的休息提供者):
I'm not sure that the provider will look bad after the change: all you have to do is to alter the UPDATE verb. By default, it looks like (for the simple rest provider):
case UPDATE:
url = `${apiUrl}/${resource}/${params.id}`;
options.method = 'PUT';
options.body = JSON.stringify(params.data);
break;
你只需要像这样更新它:
You just need to update it like so:
case UPDATE:
url = `${apiUrl}/${resource}/${params.id}`;
options.method = 'PATCH';
- options.body = JSON.stringify(params.data);
+ options.body = JSON.stringify(diff(params.data, param.previousData));
break;
其中 diff
可以写成:
const diff = (previous, current) => lodash.pickBy(current, (v, k) => previous[k] !== v);
这篇关于react-admin 排除“记录"从 redux 形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!