diff --git a/extensions/amp-form/0.1/amp-form.js b/extensions/amp-form/0.1/amp-form.js index 40c78d7ebeaf..2bcafeb71b25 100644 --- a/extensions/amp-form/0.1/amp-form.js +++ b/extensions/amp-form/0.1/amp-form.js @@ -211,9 +211,12 @@ export class AmpForm { this.renderTemplate_(error.responseJson || {}); rethrowAsync('Form submission failed:', error); }); - } else if (this.target_ == '_top' && this.method_ == 'POST') { - this.cleanupRenderedTemplate_(); - this.setState_(FormState_.SUBMITTING); + } else if (this.method_ == 'POST') { + e.preventDefault(); + user().assert(false, + 'Only XHR based (via action-xhr attribute) submissions are support ' + + 'for POST requests. %s', + this.form_); } } diff --git a/extensions/amp-form/0.1/test/test-amp-form.js b/extensions/amp-form/0.1/test/test-amp-form.js index 49b58d4f9bec..50bcf716df9c 100644 --- a/extensions/amp-form/0.1/test/test-amp-form.js +++ b/extensions/amp-form/0.1/test/test-amp-form.js @@ -149,6 +149,22 @@ describe('amp-form', () => { expect(ampForm.xhr_.fetchJson.called).to.be.false; }); + it('should throw error if POST non-xhr', () => { + const form = getForm(); + form.removeAttribute('action-xhr'); + const ampForm = new AmpForm(form); + const event = { + stopImmediatePropagation: sandbox.spy(), + target: form, + preventDefault: sandbox.spy(), + }; + sandbox.spy(ampForm.xhr_, 'fetchJson'); + sandbox.spy(form, 'checkValidity'); + expect(() => ampForm.handleSubmit_(event)).to.throw( + /Only XHR based \(via action-xhr attribute\) submissions are support/); + expect(event.preventDefault).to.be.called; + }); + it('should respect novalidate on a form', () => { setReportValiditySupported(true); const form = getForm(); @@ -176,14 +192,13 @@ describe('amp-form', () => { }; sandbox.spy(form, 'checkValidity'); sandbox.spy(emailInput, 'reportValidity'); - ampForm.xhrAction_ = null; + ampForm.handleSubmit_(event); // Check validity should always be called regardless of novalidate. expect(form.checkValidity.called).to.be.true; // However reporting validity shouldn't happen when novalidate. expect(emailInput.reportValidity.called).to.be.false; - expect(event.preventDefault.called).to.be.false; expect(form.hasAttribute('amp-novalidate')).to.be.true; }); diff --git a/extensions/amp-form/amp-form.md b/extensions/amp-form/amp-form.md index 7457f6cb7355..4078c45ab37d 100644 --- a/extensions/amp-form/amp-form.md +++ b/extensions/amp-form/amp-form.md @@ -71,18 +71,22 @@ __required__ Target attribute of the `