1

In react-admin I have the following ReferenceArrayInput -> SelectArrayInput component as following:

<Create {...props}>
    <SimpleForm>

        <ReferenceArrayInput source="clients" reference="client">
            <SelectArrayInput optionText="clientName" />
        </ReferenceArrayInput>

    </SimpleForm>
</Create>

It works, it receives the data from the client source and id displays the options with the clientName label.

However, when the form is submitted it sends to the server the following request body:

{
   "clients": [1,2,3]
{

I would like to send it as:

{
   "clients": [
     {"id": 1},
     {"id": 2},
     {"id": 3}
   ]
}

Is is possible, by setting some certain option or shall I use a different component?

1 Answer 1

2

You have two options for this:

  • Transform the data in your dataProvider

  • Pass a transform function to the transform prop of your Create or Edit component.

    export const UserCreate = (props) => {
        const transform = (data) => ({
            ...data,
            clients: data.clients.map((id) => ({ id })),
        });
    
        return (
            <Create {...props} transform={transform}>
                // Your inputs
            </Create>
        );
    };
    
Sign up to request clarification or add additional context in comments.

1 Comment

Thansk Gildas, very kind!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.