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/16611: Trim value before validation in Form Component #16861

Merged
merged 4 commits into from
Apr 7, 2023
Merged
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
13 changes: 11 additions & 2 deletions src/components/Form.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,14 +171,23 @@ class Form extends React.Component {
* @returns {Object} - An object containing the errors for each inputID, e.g. {inputID1: error1, inputID2: error2}
*/
validate(values) {
const trimmedStringValues = {};
_.each(values, (inputValue, inputID) => {
if (_.isString(inputValue)) {
(trimmedStringValues[inputID] = inputValue.trim());
} else {
trimmedStringValues[inputID] = inputValue;
}
});

FormActions.setErrors(this.props.formID, null);
FormActions.setErrorFields(this.props.formID, null);

// Run any validations passed as a prop
const validationErrors = this.props.validate(values);
const validationErrors = this.props.validate(trimmedStringValues);

// Validate the input for html tags. It should supercede any other error
_.each(values, (inputValue, inputID) => {
_.each(trimmedStringValues, (inputValue, inputID) => {
// Return early if there is no value OR the value is not a string OR there are no HTML characters
if (!inputValue || !_.isString(inputValue) || inputValue.search(CONST.VALIDATE_FOR_HTML_TAG_REGEX) === -1) {
return;
Expand Down