This repository has been archived by the owner on Apr 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 472
Client side validation of Signup form #1251
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
app/assets/javascripts/modules/users/components/signup-form.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import BaseComponent from '~/base/component'; | ||
|
||
const PASSWORD_FIELD = '#user_password'; | ||
const USERNAME_FIELD = '#user_username'; | ||
const EMAIL_FIELD = '#user_email'; | ||
const CONFIRMATION_PASSWORD_FIELD = '#user_password_confirmation'; | ||
const SUBMIT_BUTTON = '#submit_btn'; | ||
|
||
// UsersPasswordForm component that handles user password form | ||
// interactions. | ||
class UsersSignUpForm extends BaseComponent { | ||
elements() { | ||
this.$password = this.$el.find(PASSWORD_FIELD); | ||
this.$passwordConfirmation = this.$el.find(CONFIRMATION_PASSWORD_FIELD); | ||
this.$username = this.$el.find(USERNAME_FIELD); | ||
this.$userEmail = this.$el.find(EMAIL_FIELD); | ||
this.$submit = this.$el.find(SUBMIT_BUTTON); | ||
} | ||
|
||
events() { | ||
this.$el.on('focusout', PASSWORD_FIELD, e => this.onFocusout(e)); | ||
this.$el.on('focusout', CONFIRMATION_PASSWORD_FIELD, e => this.onFocusout(e)); | ||
this.$el.on('focusout', USERNAME_FIELD, e => this.onFocusout(e)); | ||
this.$el.on('focusout', EMAIL_FIELD, e => this.onFocusout(e)); | ||
} | ||
|
||
onFocusout() { | ||
const usernameInvalid = !this.$username[0].validity.valid; | ||
const emailInvalid = !this.$userEmail[0].validity.valid; | ||
const passwordInvalid = !this.$password.val() || this.$password.val().length < 8; | ||
const passwordConfirmationInvalid = !this.$passwordConfirmation.val() || | ||
this.$password.val() !== this.$passwordConfirmation.val(); | ||
|
||
if (passwordConfirmationInvalid) { | ||
this.$passwordConfirmation[0].setCustomValidity('Please enter same password as above'); | ||
} else { | ||
this.$passwordConfirmation[0].setCustomValidity(''); | ||
} | ||
|
||
if (passwordInvalid) { | ||
this.$password[0].setCustomValidity('Password should be of at least 8 characters minimum.'); | ||
} else { | ||
this.$password[0].setCustomValidity(''); | ||
this.$passwordConfirmation.attr('pattern', this.$password.val()); | ||
} | ||
|
||
if (passwordInvalid || passwordConfirmationInvalid || emailInvalid || usernameInvalid) { | ||
this.$submit.attr('disabled', true); | ||
} else { | ||
this.$submit.removeAttr('disabled'); | ||
} | ||
} | ||
} | ||
|
||
export default UsersSignUpForm; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,20 @@ | ||
import BaseComponent from '~/base/component'; | ||
|
||
import { fadeIn } from '~/utils/effects'; | ||
import SignUpForm from '../components/signup-form'; | ||
|
||
const SIGN_UP_FORM = 'form.new_user'; | ||
|
||
// UsersSignUpPage component responsible to instantiate | ||
// the user's sign up page components and handle interactions. | ||
class UsersSignUpPage extends BaseComponent { | ||
elements() { | ||
this.$signupForm = this.$el.find(SIGN_UP_FORM); | ||
} | ||
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. nitpicking: add an empty line to separate the 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. Yeah Sure 😃 |
||
|
||
mount() { | ||
fadeIn(this.$el); | ||
this.signupForm = new SignUpForm(this.$signupForm); | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
If you are not gonna use the $element, you don't need to cache it here. So, I think you can remove both
username
andemail
elements, constants and event listeners.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.
Oops. My bad! Forgot the disabled button. 😅
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.
Yeah ..they are used in
emailInvalid
,usernameInvalid
and for disabling the submit button. So no need to remove them right ?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.
No need. I just woke up. Wasn't thinking really straight yet. KKKK.