Search code examples
react-adminreact-final-form

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


The List View of react-admin provides a "create (new record)" button out of the box when I specify a Create view in the Resource.

Since my record structure is nested up to three levels, containing objects with objects with arrays a.s.o., starting with an empty record (just {}) leads to a bunch of "undefined" errors in the validation function and when I test certain values with a FormDataConsumer to fold/unfold parts of the form based on other values.

I want my Create view to always start with a predefined record structure. How can I do that?


Solution

  • So it looks like you need default values for create form.

    Documentation: 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>
    );