react-admin icon indicating copy to clipboard operation
react-admin copied to clipboard

Ignored source parameter in the url of a create form

Open alixdeschamps opened this issue 6 years ago • 7 comments

What you were expecting: I'm implementing a "Save and Add" button to allow bulk creation.

I'm trying to prefill certain fields on the next create form based on the inputs of the previous form.

I'm passing a stringified source parameter in the url (with the fields I want to prefill) in the SaveButton redirect function as suggested by @fzaninotto there

What happened instead: The next create form is prefilled with all the values from the previous form (an not only the values I specified in the source parameter)

Steps to reproduce: From the sandbox provided:

  • login
  • click to create a new post
  • fill title and teaser field
  • click on "save and add"

The new form contains not only the teaser value (specified in the source parameter) but also the title value from the previous form.

Related code: https://codesandbox.io/s/friendly-proskuriakova-yg6ib forked from https://codesandbox.io/s/github/marmelab/react-admin/tree/master/examples/simple

SaveButton snippet from PostCreate.js :

<SaveButton
      label="post.action.save_and_add"
      redirect={(redirectTo, basePath, is, data) =>
        `/posts/create?source={"title":"previous title"}`
      }
      submitOnEnter={false}
      variant="text"
    />

Environment

  • React-admin version: 3.0.0
  • React version: 16.9.0

alixdeschamps avatar Feb 05 '20 15:02 alixdeschamps

Thanks for your report.

What you describe is a known limitation of the current react-admin creation form. The "save and add" feature is implemented by setting the redirect prop to false (see https://github.com/marmelab/react-admin/blob/master/packages/ra-core/src/sideEffect/useRedirect.ts), so you can't use this mechanism to have a prefilled form.

But, if you change the "create" button in the list by a link to the prefilled form using the source query parameter, and set redirect to false in the SaveButton, the save and add will refill the form with the source every time (including the first). I don't know if it fits your use case. If it does not, you'll have to write your own creation form and save handler based on React Final Form.

That being said, I don't know if we want that use case to be managed by react-admin. Leaving this issue open for discussion.

fzaninotto avatar Feb 06 '20 23:02 fzaninotto

Maybe you could create your own button redirecting to the create page with what you want on the query string. Something like:

<Button color="primary" href={`${basePath}/create?source={${...........}`}>
   <AddIcon /> Save and add
</Button>

Complementing: https://stackoverflow.com/questions/59038813/manipulate-create-button-path

liviozanol avatar Feb 08 '20 10:02 liviozanol

Thanks for you suggestions.

@fzaninotto replacing the create button in the list view wouldn't work because I want the source to be based on the previously entered data. This data is not available from the list view as far as I know.

I could write my own component. Would I need to write the whole form with React Final Form or just a new Button component?

I am interested if someone has an example of code to see which part should i customise.

alixdeschamps avatar Feb 10 '20 09:02 alixdeschamps

@liviozanol thanks too, similar suggestion as francois, but still doesn't fit my need (see previous message)

alixdeschamps avatar Feb 10 '20 09:02 alixdeschamps

@alixdeschamps in this case, can you render the create button based on a query to the API? If so, you could still use the example I've provided.

In this specific case you mentioned, I can't see a reliable solution just changing the main "create" button. I think it will be susceptible to race condition, and it should depend on the backend data.

Anyway, maybe you could create your own "save and add" button, also using the example provided, making the href/refirect based on the current form. You could use, for example, "onchange" in the form itself to change the path on href/redirect as the form is filled.

liviozanol avatar Feb 10 '20 11:02 liviozanol

Please use StackOverflow for "how to" questions. This tracker is for bugs only.

fzaninotto avatar Feb 10 '20 12:02 fzaninotto

OK, closing this issue as it's in fact a support question for how to implement a feature that react-admin doesn't provide. Our answer is, as usual: if you can't do it with react-admin, do it without react-admin (using react-final-form directly).

fzaninotto avatar Mar 19 '20 13:03 fzaninotto