Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: prevent browser form validation #24428

Merged
merged 4 commits into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion src/components/FormSubmit/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import React from 'react';
import React, {useEffect} from 'react';
import lodashGet from 'lodash/get';
import {View} from 'react-native';
import * as formSubmitPropTypes from './formSubmitPropTypes';
import CONST from '../../CONST';
import isEnterWhileComposition from '../../libs/KeyboardShortcut/isEnterWhileComposition';
import * as ComponentUtils from '../../libs/ComponentUtils';

function FormSubmit({innerRef, children, onSubmit, style}) {
/**
Expand Down Expand Up @@ -36,11 +37,31 @@ function FormSubmit({innerRef, children, onSubmit, style}) {
}
};

const preventDefaultFormBehavior = (e) => e.preventDefault();

useEffect(() => {
const form = innerRef.current;

// Prevent the browser from applying its own validation, which affects the email input
form.setAttribute('novalidate', '');

form.addEventListener('submit', preventDefaultFormBehavior);

return () => {
if (!form) {
return;
}

form.removeEventListener('submit', preventDefaultFormBehavior);
};
}, [innerRef]);

return (
// React-native-web prevents event bubbling on TextInput for key presses
// https://github.com/necolas/react-native-web/blob/fa47f80d34ee6cde2536b2a2241e326f84b633c4/packages/react-native-web/src/exports/TextInput/index.js#L272
// Thus use capture phase.
<View
accessibilityRole={ComponentUtils.ACCESSIBILITY_ROLE_FORM}
ref={innerRef}
onKeyDownCapture={submitForm}
style={style}
Expand Down
3 changes: 3 additions & 0 deletions src/components/SignInPageForm/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ class Form extends React.Component {
return;
}

// Prevent the browser from applying its own validation, which affects the email input
this.form.setAttribute('novalidate', '');

// Password Managers need these attributes to be able to identify the form elements properly.
this.form.setAttribute('method', 'post');
this.form.setAttribute('action', '/');
Expand Down
Loading