-
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
Conversation
Hey! I see that you made changes to our Form component. Make sure to update the docs in FORMS.md accordingly. Cheers! |
@jasperhuangg @rushatgabhane One of you needs to copy/paste the Reviewer Checklist from here into a new comment on this PR and complete it. If you have the K2 extension, you can simply click: [this button] |
src/components/Form.js
Outdated
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 comment
The 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 comment
The 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.
We can replace _.isString(inputValue)
into !_.isEmpty(inputValue)
then it will check for both string and non-string values
But I don't see any values are non-string in Form
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.
But I don't see any values are non-string in Form
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.
src/components/Form.js
Outdated
@@ -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())); |
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.
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 crash
And I just updated _.isString(inputValue)
into !_.isEmpty(inputValue)
So it also resolved this problem because _.isEmpty(boolean) === true
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.
@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
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.
looks good and tests well :D
@rushatgabhane Friendly bump, I think this is ready to go, do you mind completing the reviewer checklist and approving when you get the chance? |
Reviewer Checklist
Screenshots/Videos |
@jasperhuangg LGTM as well |
@jasperhuangg Friendly bump, This PR is ready to merge |
@jasperhuangg looks like this was merged without a test passing. Please add a note explaining why this was done and remove the |
PR reviewer checklist was clearly completed above, not an emergency. |
✋ This PR was not deployed to staging yet because QA is ongoing. It will be automatically deployed to staging after the next production release. |
🚀 Deployed to staging by https://github.com/jasperhuangg in version: 1.2.97-0 🚀
|
🚀 Deployed to production by https://github.com/francoisl in version: 1.2.97-2 🚀
|
Details
We should trim the value before validation in Form Component. This PR does that
Fixed Issues
$ #16611
PROPOSAL: #16611 (comment)
Tests
Offline tests
Same above
QA Steps
PR Author Checklist
### Fixed Issues
section aboveTests
sectionOffline steps
sectionQA steps
sectiontoggleReport
and notonIconClick
)src/languages/*
files and using the translation methodWaiting for Copy
label for a copy review on the original GH to get the correct copy.STYLE.md
) were followedAvatar
, I verified the components usingAvatar
are working as expected)/** comment above it */
this
properly so there are no scoping issues (i.e. foronClick={this.submit}
the methodthis.submit
should be bound tothis
in the constructor)this
are necessary to be bound (i.e. avoidthis.submit = this.submit.bind(this);
ifthis.submit
is never passed to a component event handler likeonClick
)StyleUtils.getBackgroundAndBorderStyle(themeColors.componentBG
)Avatar
is modified, I verified thatAvatar
is working as expected in all cases)ScrollView
component to make it scrollable when more elements are added to the page.main
branch was merged into this PR after a review, I tested again and verified the outcome was still expected according to theTest
steps.Screenshots/Videos
Web
Screen.Recording.2023-04-02.at.11.43.40.mov
Mobile Web - Chrome
Screen.Recording.2023-04-02.at.11.44.44.mov
Mobile Web - Safari
Screen.Recording.2023-04-02.at.12.35.32.mov
Desktop
Screen.Recording.2023-04-02.at.11.46.53.mov
iOS
Screen.Recording.2023-04-02.at.11.44.12.mov
Android
Screen-Recording-2023-04-02-at-12.28.38.mp4