From 67dc2685a72c125b53f351c3a75bf812123e96bd Mon Sep 17 00:00:00 2001 From: Azul Date: Mon, 12 Nov 2012 13:03:13 +0100 Subject: fixed signup bug and refactored a bit --- users/app/assets/javascripts/users.js.coffee | 51 ++++++++++++++++------------ 1 file changed, 29 insertions(+), 22 deletions(-) (limited to 'users/app/assets') diff --git a/users/app/assets/javascripts/users.js.coffee b/users/app/assets/javascripts/users.js.coffee index 24302fe..8a9f0e9 100644 --- a/users/app/assets/javascripts/users.js.coffee +++ b/users/app/assets/javascripts/users.js.coffee @@ -3,43 +3,50 @@ # You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/ # -validate_password = (event) -> +preventDefault = (event) -> + event.preventDefault() - password = $('#srp_password').val() - confirmation = $('#srp_password_confirmation').val() - login = $('#srp_username').val() - - if password != confirmation - alert "Password and Confirmation do not match!" - $('#srp_password').focus() - return false - if password == login - alert "Password and Login may not match!" - $('#srp_password').focus() - return false - if password.length < 8 - alert "Password needs to be at least 8 characters long!" - $('#srp_password').focus() - return false +validOrAbort = (event) -> + errors = {} + + abortIfErrors = -> + return if $.isEmptyObject(errors) + $.each errors, (field, error) -> + alert(error) + $('#srp_password').focus() + event.stopImmediatePropagation() + + validatePassword = -> + password = $('#srp_password').val() + confirmation = $('#srp_password_confirmation').val() + login = $('#srp_username').val() + + if password != confirmation + errors.password_confirmation = "Confirmation does not match!" + if password == login + errors.password = "Password and Login may not match!" + if password.length < 8 + errors.password = "Password needs to be at least 8 characters long!" + + validatePassword() + abortIfErrors() - return true - signup = (event) -> srp = new SRP(jqueryRest()) srp.register -> window.location = '/' - false login = (event) -> srp = new SRP(jqueryRest()) srp.identify -> window.location = '/' - false $(document).ready -> - $('#new_user').submit validate_password + $('#new_user').submit preventDefault + $('#new_user').submit validOrAbort $('#new_user').submit signup + $('#new_session').submit preventDefault $('#new_session').submit login -- cgit v1.2.3 From 6ba3366f778340ebeaa73fd53372368b16de6c98 Mon Sep 17 00:00:00 2001 From: Azul Date: Tue, 13 Nov 2012 12:00:13 +0100 Subject: using client side validations during signup --- users/app/assets/javascripts/users.js.coffee | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) (limited to 'users/app/assets') diff --git a/users/app/assets/javascripts/users.js.coffee b/users/app/assets/javascripts/users.js.coffee index 8a9f0e9..ab437f6 100644 --- a/users/app/assets/javascripts/users.js.coffee +++ b/users/app/assets/javascripts/users.js.coffee @@ -1,8 +1,3 @@ -# Place all the behaviors and hooks related to the matching controller here. -# All this logic will automatically be available in application.js. -# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/ -# - preventDefault = (event) -> event.preventDefault() @@ -11,9 +6,9 @@ validOrAbort = (event) -> abortIfErrors = -> return if $.isEmptyObject(errors) - $.each errors, (field, error) -> - alert(error) - $('#srp_password').focus() + # we're relying on client_side_validations here instead of printing + # our own errors. This gets us translatable error messages. + $('.control-group.error input, .control-group.error select, control-group.error textarea').first().focus() event.stopImmediatePropagation() validatePassword = -> -- cgit v1.2.3