如何在 react-admin 的列表视图中的创建按钮上预填充复杂嵌套表单的结构?

How can I prepopulate the structure of a complex nested form on the Create button in a List view in react-admin?

当我在 Resource 中指定 Create 视图时,react-admin 的 List 视图提供了一个开箱即用的 "create (new record)" 按钮。

由于我的记录结构最多嵌套三层,包含对象和对象的数组a.s.o,从一个空记录开始(只是{})导致一堆"undefined" 验证函数中的错误以及当我根据其他值使用 FormDataConsumer 到 fold/unfold 部分表单测试某些值时。

我希望我的 Create 视图始终以预定义的记录结构开始。我该怎么做?

您可以展平所有的嵌套结构,将输入的数据还原回提交前的嵌套结构。本文档可能对您有所帮助:https://marmelab.com/react-admin/CreateEdit.html#altering-the-form-values-before-submitting

看来您需要默认值来创建表单。

文档https://marmelab.com/react-admin/CreateEdit.html#default-values

const postDefaultValue = { created_at: new Date(), nb_views: 0 };
export const PostCreate = (props) => (
    <Create {...props}>
        <SimpleForm initialValues={postDefaultValue}>
            <TextInput source="title" />
            <RichTextInput source="body" />
            <NumberInput source="nb_views" />
        </SimpleForm>
    </Create>
);