question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Use `action` attribute on `form` element.

See original GitHub issue

Bug, Feature, or Question?

Question

Stupid question but is it possible to use action='/api' method='POST' instead of ajax? I am trying to use it but its not working for me. Is there a prop I have to supply?

  • Formik Version: 0.11.11
  • React Version: 16.2.0
  • TypeScript Version: N/A
  • CodeSandbox Link: N/A
  • OS: Mac OS X 10.13.3
  • Node Version: 8.0.0
  • Package Manager and Version: NPM, 5.8.0

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:20

github_iconTop GitHub Comments

56reactions
seanconnollydevcommented, Mar 12, 2019

I feel sorry for anyone who stumbles across this thread because they are in for a world of confusion.

The answer to the original questions is yes you can use action='/api' method='POST' instead of ajax and the working demo is here: https://codesandbox.io/s/826qv6lv7l ✌️

41reactions
seanconnollydevcommented, Mar 12, 2019

What’s the point in using Formik if you don’t want to submit your form data with JavaScript?

Glad you asked! Straight from Formik’s README:

Formik is a small library that helps you with the 3 most annoying parts:
1. Getting values in and out of form state
2. Validation and error messages
3. Handling form submission

Even when using HTML forms, one can still draw value from Formik for 1 and 2.

Formik puts your from values in memory so you can use them easily with JS/React. If you want to do a regular form POST action it will use the values that are in the DOM. These are two completely different things and you can’t mix them together.

I disagree with your premise here. Formik is an extension of React and React is designed to control the DOM. Even the React Forms docs show that React itself doesn’t get in the way of initiating the default form submission event. Quite the opposite in fact, because you the developer need to explicitly call e.preventDefault() if you want to handle the form submission via xhr.

One reason why Formik prevents the default form submission is because Formik supports asynchronous validation via the validate function. If the form’s onSubmit returns, then the default submission would occur before the async validation has a chance to run. Given that, the only way that I can see to use Formik with HTML form posts is to submit the form via javascript yourself:

const formEl = useRef(null);

...

 const handleSubmit = values => {
  formEl.current.submit();
};

...

<form action="/path" ref={formEl}> ...

Full demo here: https://codesandbox.io/s/826qv6lv7l

Read more comments on GitHub >

github_iconTop Results From Across the Web

HTML form action Attribute - W3Schools
The action attribute specifies where to send the form-data when a form is submitted. Browser Support. Attribute. action, Yes, Yes, Yes, Yes, Yes ......
Read more >
HTML form action Attribute - Dofactory
The action attribute on a <form> tag specifies the form-handler that will process the submitted form data. Form data is submitted to a...
Read more >
HTML| action Attribute - GeeksforGeeks
The HTML | action Attribute is used to specify where the form data is to be sent to the server after the submission...
Read more >
The Form element - HTML: HyperText Markup Language | MDN
Attributes for form submission · If the value of the method attribute is post , enctype is the MIME type of the form...
Read more >
Setting the Form Action with a JavaScript Function
In an HTML form, the action attribute is used to indicate where the form's data is sent to when it is submitted. The...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found