-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -171,14 +171,16 @@ class Form extends React.Component { | |
* @returns {Object} - An object containing the errors for each inputID, e.g. {inputID1: error1, inputID2: error2} | ||
*/ | ||
validate(values) { | ||
const trimmedValues = {}; | ||
_.each(values, (inputValue, inputID) => _.isString(inputValue) && (trimmedValues[inputID] = inputValue.trim())); | ||
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(trimmedValues); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will the errors for non-string values will still be validated? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @rushatgabhane I am checking if it is a string value, it will be trimmed. So that non-string values will not be trimmed but it still is validated as before. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
agree with you. but then there's a reason you're checking if the value is string before trimming. Form is a generic component so I think we should handle non string values as well. |
||
|
||
// Validate the input for html tags. It should supercede any other error | ||
_.each(values, (inputValue, inputID) => { | ||
_.each(trimmedValues, (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; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
won't this crash the app when we're trimming an empty string?
emptyString = ''
isString(emptyString) is true
emptyString.trim() ---> crash 💥
This comment was marked as outdated.
Sorry, something went wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh wait me, I need to check again
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rushatgabhane
I don't see that
emptyString.trim() ---> crash 💥
, emptyString.trim() = ""The crash app can be caused from some boolean fields
In the first commit, I added the condition
_.isString(inputValue)
to avoid the crashAnd I just updated
_.isString(inputValue)
into!_.isEmpty(inputValue)
So it also resolved this problem because _.isEmpty(boolean) === trueThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rushatgabhane Bump
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tienifr ah i see there's a misunderstanding
here is what i mean
allValues = [...trimmedStringValues, ...nonTrimmedNonStringValues]
vs what we're doing right now -
allValues = [...trimmedStringValues]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not suggesting that we should trim non-string values.
We're completely dropping non-string values. I'm suggesting that we don't drop them, pass them as they are
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rushatgabhane Oh, my bad 😄 Just updated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rushatgabhane bump
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rushatgabhane Could you help to take a look at this PR? It is given an approval