javascript - How to set a form as "pristine" after submit was successful in React Final Form - Stack Overflow

As a developer I would like to disable the "Submit" button when the user successfully submitt

As a developer I would like to disable the "Submit" button when the user successfully submitted the form using React Final Form, and enable the "Submit" button if the user enters some new text in the form.

Current

The variable pristine sets the disable prop to true only when the form loads for first time, but when the user submit the form (not reloading the page) the variable pristine is false and I expect it to be true in order to disable the "Submit" button.

Desired behaviour

  • The Submit button is disabled when the form loads for first time.
  • The Submit button is enabled when the user enters text or make changes.
  • The Submit button is disabled after the user click Submit and it was successful.
  • The Submit button should be enabled if the user enters more changes after submitting.

Example of current behaviour

My investigation

I tried to use a bination of the following variables provided by Final Form but with unsuccessful results pristine, touched, submitSucceeded, lastSubmittedValues

As a developer I would like to disable the "Submit" button when the user successfully submitted the form using React Final Form, and enable the "Submit" button if the user enters some new text in the form.

Current

The variable pristine sets the disable prop to true only when the form loads for first time, but when the user submit the form (not reloading the page) the variable pristine is false and I expect it to be true in order to disable the "Submit" button.

Desired behaviour

  • The Submit button is disabled when the form loads for first time.
  • The Submit button is enabled when the user enters text or make changes.
  • The Submit button is disabled after the user click Submit and it was successful.
  • The Submit button should be enabled if the user enters more changes after submitting.

Example of current behaviour

https://codesandbox.io/s/github/final-form/react-final-form/tree/master/examples/simple

My investigation

I tried to use a bination of the following variables provided by Final Form but with unsuccessful results pristine, touched, submitSucceeded, lastSubmittedValues

Share Improve this question asked Feb 29, 2020 at 1:48 MattMatt 6494 gold badges12 silver badges20 bronze badges 2
  • do you have any code? – ezra Commented Feb 29, 2020 at 2:07
  • @ezra I provided the code, it is in here: codesandbox.io/s/github/final-form/react-final-form/tree/master/… – Matt Commented Feb 29, 2020 at 3:18
Add a ment  | 

1 Answer 1

Reset to default 6

To acplish this, you will need to "reinitialize" the form after you submit.

After your submit is successful, you will need to pass those submitted form values back into the initialValues prop on <Form/>. That will ensure that your pristine value accurately reflects the values that have been saved/submitted.

If you do this, pristine should be the only value your submit button needs to know if it should be disabled or not.


Side note: Please only implement this pattern on forms that are editing existing data. The reason being that if you are starting with an empty form, and have required fields, attempting to submit is helpful in that it will mark all the fields as touched so you can display error messages. But if you disable the submit button on pristine, you cannot get this benefit.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744620849a4584334.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信