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

Revert signup validation changes #8466

Merged
merged 1 commit into from
Oct 6, 2020
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
43 changes: 29 additions & 14 deletions app/assets/javascripts/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,21 @@ $(document).ready(function() {
$(document).ready(function() {
// The two forms have the same ID
var signUpForms = document.querySelectorAll("#create-form");
var signUpErrorMessages = document.querySelector("#error-message");

// Sign up modal form
signUpForms[0].classList.add("signup-modal-form");
var signUpModalForm = new SignUpFormValidator(".signup-modal-form");

// publiclab.org/register form
if (signUpForms[1]) {
signUpForms[1].classList.add("signup-register-form");
var signUpRegisterForm = new SignUpFormValidator(".signup-register-form");
signUpRegisterForm.validateForm()

if (signUpErrorMessages.innerHTML.includes("Email")) {
signUpRegisterForm.updateUI(signUpRegisterForm.emailElement, false, "Email already exists");
}
signUpRegisterForm.isFormValid()
}

// The same goes for login forms
Expand Down Expand Up @@ -133,39 +139,40 @@ function SignUpFormValidator(formClass) {
if (!signUpForm) return;

this.validationTracker = {};
this.submitBtn = document.querySelector(formClass + ' [type="submit"');

this.submitBtn = document.querySelector(formClass + ' [type="submit"]');

this.isFormValid();

var usernameElement = document.querySelector(
this.usernameElement = document.querySelector(
formClass + " [name='user[username]']"
);
var emailElement = document.querySelector(
this.emailElement = document.querySelector(
formClass + " [name='user[email]']"
);
var passwordElement = document.querySelector(
this.passwordElement = document.querySelector(
formClass + " [name='user[password]']"
);
var confirmPasswordElement = document.querySelector(
this.confirmPasswordElement = document.querySelector(
formClass + " [name='user[password_confirmation]']"
);

// Every time user types something, corresponding event listener are triggered
usernameElement.addEventListener(
this.usernameElement.addEventListener(
"input",
validateUsername.bind(usernameElement, this)
validateUsername.bind(this.usernameElement, this)
);
emailElement.addEventListener(
this.emailElement.addEventListener(
"input",
validateEmail.bind(emailElement, this)
validateEmail.bind(this.emailElement, this)
);
passwordElement.addEventListener(
this.passwordElement.addEventListener(
"input",
validatePassword.bind(passwordElement, confirmPasswordElement, this)
validatePassword.bind(this.passwordElement, this.confirmPasswordElement, this)
);
confirmPasswordElement.addEventListener(
this.confirmPasswordElement.addEventListener(
"input",
validateConfirmPassword.bind(confirmPasswordElement, passwordElement, this)
validateConfirmPassword.bind(this.confirmPasswordElement, this.passwordElement, this)
);
}

Expand Down Expand Up @@ -195,6 +202,13 @@ SignUpFormValidator.prototype.enableSubmitBtn = function() {
this.submitBtn.removeAttribute("disabled");
};

SignUpFormValidator.prototype.validateForm = function() {
validateUsername.call(this.usernameElement, this)
validateEmail.call(this.emailElement, this)
validatePassword.call(this.passwordElement, this.confirmPasswordElement, this)
validateConfirmPassword.call(this.confirmPasswordElement, this.passwordElement, this)
};

SignUpFormValidator.prototype.isFormValid = function() {
// Form is valid if all elements have passsed validation successfully
var isValidForm =
Expand Down Expand Up @@ -242,6 +256,7 @@ function validateEmail(obj) {
}

function validatePassword(confirmPasswordElement, obj) {

var password = this.value.trim();

if (password.length === 0) {
Expand Down
4 changes: 2 additions & 2 deletions app/views/users/_create_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div style="padding:10px;">
<%= form_for :user, :as => :user, :url => "/register", :html => {:class => "container", :id => "create-form", :'data-toggle' => "validator"} do |f| %>
<% if f.error_messages != "" %><div class="alert alert-danger"><%= f.error_messages %></div><% end %>
<% if f.error_messages != "" %><div class="alert alert-danger" id="error-message"><%= f.error_messages %></div><% end %>

<input type="hidden" name="return_to" class="login-modal-redirect" value="<%= params[:return_to] || request.fullpath %>" />

Expand Down Expand Up @@ -110,7 +110,7 @@

<div class="col-md-12 form-group form-inline" style="clear:both;padding-top:10px;">
<!-- button for creating new users -->
<button style="margin-right:8px;" class="btn btn-lg btn-primary btn-save sranje" type="submit" tabindex="7"><%= translation('users._form.sign_up') %></button>
<button style="margin-right:8px;" class="btn btn-lg btn-primary btn-save sranje" id="signup-button" type="submit" tabindex="7"><%= translation('users._form.sign_up') %></button>
<span>or <a id="toLogin" href="/login">
<%= translation('users._form.log_in') %>
</a>
Expand Down