Netlify Forms with Vue @submit.prevent="handleSubmit"

Hey @cfjedimaster,
Glad to hear you sorted out your Vue form! To make sure I understand, the next issue you raised is not about Vue, but about submitting a vanilla Javascript form using fetch. Is that right? Let us know and we’ll dig in.

Correct - it isn’t a Vue issue, but an “Ajax submit on Netlify” issue. I spoke with Brian Rinaldi and he mentioned he saw this as well.

To test, post with forn name equal to Something. It will fail. Build a simple form that just does a regular post and uses the same name. Now the fancy Ajax one will work.

Hi, @cfjedimaster, this sounds a bit like the “HTML form must exist” requirement mentioned here. Quoting:

If you’re using a pure javascript form instead, this additional step is required:

  • You still need to include an HTML form that meets the criteria above, including all the inputs with the same names as the javascript form. This is required because we parse HTML to find forms during your deploy - but we don’t try to parse javascript files. This means if you are using React, Vue, etc. which create HTML elements using Javascript during the render, you’ll still need to include an actual HTML file that includes all of the form details in it, including all possible inputs that you want to use. For a working example using React that you can generalize to other frameworks from, check out this blog post .

It sounds like the requirement above causing the behavior mentioned here?

I ask because it sounds like this is the issue:

  • javascript form doesn’t work
  • HTML form is created
  • javascript starts working

If this is the issue, that is because the HTML form is required for all forms. If I’ve misunderstood the issue, please let us know.

Close, but I am using an HTML form. Here it is:

This did not work until I built:

This is the same form but with no Ajax. As soon as I submitted that one the Ajax one worked, but again, you can see the ‘regular’ form in form3.html as well, it just uses Ajax to submit it.

Hey @cfjedimaster,
Since this is not about Vue, but about submitting a form using fetch, I don’t think it’s useful to test with Vue as you’re doing in the first example. This example works for me on first submit, without sending a non-fetch form first:

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Special Contact Form</title>
    <h2>Contact Form</h2>
    <form method="post" name="Contact Form" id="myForm" data-netlify="true">
        <label for="name">Name:</label>
        <input type="text" name="name" id="name">
        <label for="email">Email:</label>
        <input type="email" name="email" id="email">
        <label for="comments">Comments:</label><br/>
        <textarea name="comments" id="comments"></textarea>
        <button type="submit">Submit</button>
    const handleSubmit = (e) => {
      let myForm = document.getElementById('myForm');
      let formData = new FormData(myForm)
      fetch('/', {
    	method: 'POST',
    	body: formData
    	.then(() => alert('success!'))
    	.catch((error) => alert(error))

    document.querySelector("form").addEventListener("submit", handleSubmit);

Please let us know if you’re seeing something different.

Fascinating - it did. Now - while I don’t think my Vue code is perfect, I honestly didn’t expect an issue there. And oddly, by using a new name and it working, maybe it’s something else. Doing a test now.

Nope, it still won’t work for me. I noticed you post to /. I tried that as well and it doesn’t help. I also notice you aren’t encoding your formdata - I’ll give that a shot next.

Also - if you are copying the form data with FormData(myForm), you are not passing a value called form-name with the name of the form. I thought that was required?

EDIT: Never mind - forgot that the hidden form field is added automatically.

AHAH! That’s getting me somewhere. When I view source on form3.html vs form5.html (what I called your code), your code has the hidden form tag. Netlify is not “seeing” my form tag:

<form method="post" name="My Contact Form 7" action="/" id="myForm"  @submit.prevent="sendForm" ref="formTag" data-netlify="true">

Why would it miss it?

EDIT! AHAH! So when I remove @submit.prevent="sendForm" ref="formTag" Netlify “sees” the form and adds the hidden form tag. Of course that breaks the Vue application. So is the regex that yall use somehow ‘confused’ by the Vue-ism? It shouldn’t be - but that would be a real bug you could test, right?

EDIT: It looks to be specifically @submit.prevent="sendForm" that breaks the regex.

Hey @cfjedimaster,
We just tested NetlifyTestingZone/form3.html at master · cfjedimaster/NetlifyTestingZone · GitHub and the submission was successfully received. Please let us know if you’re seeing something different.

That’s because I updated it with a fix. :slight_smile: Can you please test with the original code where @submit is used in the form tag? I believe this breaks the regex (I assume) you look when scanning for netlify forms.

Hey @cfjedimaster,
Sorry for the delay here, got a bit lost in all the different issues and form versions here. Could you please share a live version of the form you would like us to investigate? Thanks!

New form is here: NetlifyTestingZone/form10.html at master · cfjedimaster/NetlifyTestingZone · GitHub
Online it is here: Special Contact Form

If you view source, you will see Netlify didn’t “recognize” the form tag needed to be processed.

Hey @cfjedimaster,
This is what @luke was getting at earlier: that because you’re no longer submitting a pure HTML form at this point (that’s what you’re doing by adding Javascript inside the form tag), you will need to also include a pure HTML replica of the form in order for our form bots to pick it up:

So the body of your html file would look like this:

	<h2>Contact Form</h2>

	<form type="hidden" name="My Contact Form 10" data-netlify="true" >
		<input type="hidden" name="name">
		<input type="hidden" name="email">
		<input type="hidden" name="comments">

	<form method="post" name="My Contact Form 10" action="/" id="myForm" @submit.prevent="sendForm" ref="formTag">
		<label for="name">Name:</label>
		<input type="text" name="name" id="name" v-model="name">
		<label for="email">Email:</label>
		<input type="email" name="email" id="email" v-model="email">
		<label for="comments">Comments:</label><br/>
		<textarea name="comments" id="comments" v-model="comments"></textarea>
		<input type="submit">

I think about Netlify forms as having two different components:

  1. we need a version of your form that is legible to our HTML parser, that we can use to say “we’re expecting submissions to the form with x name, in y shape”;
  2. and then, apart from that, you need code to handle inputs and send them to that form that we set up for you, in the format that we expect.

In a perfect world (i.e., with pure HTML forms), these two components live in the same form, with very little additional configuration. But when they don’t, we have to do things like add a hidden form as I did above.

Let us know if this helps or if you have other questions.

Respectfully, I don’t think you are right - or I’m doing a poor job of explaining myself. :wink: I was able to get one form working with Vue just fine. Everything started working when I removed @submit.prevent from the form tag. I noticed in View Source that with that portion gone, Netlify would fine the form, add the hidden form field, and things worked perfectly fine.

If you look at NetlifyTestingZone/form3.html at master · cfjedimaster/NetlifyTestingZone · GitHub, you can see I have one form and use Vue to process it with Netlify getting the form submission fine.

If I had to guess, your code looks for netlify or data-netlify in <form> tags, and the @submit part breaks it.

Does that make sense? To be clear, I think your solution would work. But if it’s really just a regex issue perhaps, that could be fixed maybe, right?

Hi there,

we haven’t forgotten about this - we’ll get back to you soon as soon with more info.

Thanks for your patience! We finally took that exact question to our developers and they said “ah, yes, we’ll fail to process, if any attribute in the form definition starts with @ or other special characters”.

I don’t think we’ll be changing that behavior, so hopefully you can work around it.

I was able to - but this is really something that should be documented. I don’t know React, but I’d imagine this would impact multiple front end technologies that use templating. It would be a quick one line addition to the docs I’d be happy to write. You can copy and paste. Etc etc. :slight_smile: Please do so!

Thanks for the suggestion!

We’ll work on getting it into the docs, and we also got a feature request filed to allow this config, which we’ll follow up here about in case we implement it.

I just had a similar issue but with using Alpine.js directives rather than Vue ones:


I’m not sure which line caused the issue, but removing the two attributes/directives above caused my form to be discovered, whereas previously it hadn’t been.

The comment:

if any attribute in the form definition starts with @ or other special characters

From @fool earlier suggests the form will fail to be discovered if an attribute starts with a special character (guessing this means one that isn’t in the A-Z range?), but is it maybe the case that the form will also fail to be discovered if the attribute contains a special character?

Edit: as a workaround I removed those Alpine attributes, did a deployment, then checked my form had been discovered. I then added the Alpine attributes back, and did a deployment. The form remained listed in the Netlify UI, and submissions were recorded correctly. This seems like an okay workaround, as long as I don’t ever need to make a new Netlify project and move the codebase to it (if I did this I’d have to remember to remove the attributes, deploy, and then add them back and deploy again).

thanks for posting your workaround!