Skip to content

Commit

Permalink
Respect novalidate and do not check validation or stop submission. (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
mkhatib authored Jun 10, 2016
1 parent 90ea248 commit b437c96
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/document-submit.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,15 @@ export function onDocumentFormSubmit_(e) {
user.assert(target, 'form target attribute is required: %s', form);
user.assert(target == '_blank' || target == '_top',
'form target=%s is invalid can only be _blank or _top: %s', target, form);
const shouldValidate = !form.hasAttribute('novalidate');

// Safari does not trigger validation check on submission, hence we
// trigger it manually. In other browsers this would never execute since
// the submit event wouldn't be fired if the form is invalid.
// TODO: This doesn't display the validation error messages. Safari makes them
// available per input.validity object. We need to figure out a way of
// displaying these.
if (form.checkValidity && !form.checkValidity()) {
if (shouldValidate && form.checkValidity && !form.checkValidity()) {
e.preventDefault();
return;
}
Expand Down
8 changes: 8 additions & 0 deletions test/functional/test-document-submit.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@ describe('test-document-submit onDocumentFormSubmit_', () => {
expect(tgt.checkValidity.callCount).to.equal(1);
});

it('should not check validity if novalidate provided', () => {
tgt.setAttribute('novalidate', '');
tgt.checkValidity = sandbox.stub().returns(false);
onDocumentFormSubmit_(evt);
expect(preventDefaultSpy.callCount).to.equal(0);
expect(tgt.checkValidity.callCount).to.equal(0);
});

it('should not prevent default', () => {
tgt.checkValidity = sandbox.stub().returns(true);
onDocumentFormSubmit_(evt);
Expand Down

0 comments on commit b437c96

Please sign in to comment.