diff --git a/common/static/js/spec_helpers/edx.utils.validate.js b/common/static/js/spec_helpers/edx.utils.validate.js new file mode 100644 index 0000000000..293d881ee7 --- /dev/null +++ b/common/static/js/spec_helpers/edx.utils.validate.js @@ -0,0 +1,71 @@ +var edx = edx || {}; + +(function( $, _ ) { + 'use strict'; + + edx.utils = edx.utils || {}; + + var utils = (function(){ + var _fn = { + validate: { + + field: function( el ) { + var $el = $(el); + + return _fn.validate.required( $el ) && + _fn.validate.charLength( $el ) && + _fn.validate.email.valid( $el ); + }, + + charLength: function( $el ) { + // Cannot assume there will be both min and max + var min = $el.attr('minlength') || 0, + max = $el.attr('maxlength') || false, + chars = $el.val().length, + within = false; + + // if max && min && within the range + if ( min <= chars && ( max && chars <= max ) ) { + within = true; + } else if ( min <= chars && !max ) { + within = true; + } + + return within; + }, + + required: function( $el ) { + return $el.attr('required') ? $el.val() : true; + }, + + email: { + // This is the same regex used to validate email addresses in Django 1.4 + regex: new RegExp( + [ + '(^[-!#$%&\'*+/=?^_`{}|~0-9A-Z]+(\\.[-!#$%&\'*+/=?^_`{}|~0-9A-Z]+)*', + '|^"([\\001-\\010\\013\\014\\016-\\037!#-\\[\\]-\\177]|\\\\[\\001-\\011\\013\\014\\016-\\177])*"', + ')@((?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\\.)+[A-Z]{2,6}\\.?$)', + '|\\[(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}\\]$' + ].join(''), 'i' + ), + + valid: function( $el ) { + return $el.data('email') ? _fn.validate.email.format( $el.val() ) : true; + }, + + format: function( str ) { + return _fn.validate.email.regex.test( str ); + } + } + } + }; + + return { + validate: _fn.validate.field + }; + + })(); + + edx.utils.validate = utils.validate + +})( jQuery, _ ); \ No newline at end of file diff --git a/lms/envs/common.py b/lms/envs/common.py index 595edf30e4..dc1ec56026 100644 --- a/lms/envs/common.py +++ b/lms/envs/common.py @@ -1026,8 +1026,11 @@ instructor_dash_js = sorted(rooted_glob(PROJECT_ROOT / 'static', 'coffee/src/ins # These are not courseware, so they do not need many of the courseware-specific # JavaScript modules. student_account_js = [ + 'js/common_helpers/edx.utils.validate.js', 'js/student_account/models/LoginModel.js', + 'js/student_account/models/RegisterModel.js', 'js/student_account/views/LoginView.js', + 'js/student_account/views/RegisterView.js', 'js/student_account/views/AccessView.js', 'js/student_account/accessApp.js', ] diff --git a/lms/static/js/student_account/models/RegisterModel.js b/lms/static/js/student_account/models/RegisterModel.js new file mode 100644 index 0000000000..08ca2ec6d8 --- /dev/null +++ b/lms/static/js/student_account/models/RegisterModel.js @@ -0,0 +1,60 @@ +var edx = edx || {}; + +(function($, _, Backbone, gettext) { + 'use strict'; + + edx.student = edx.student || {}; + edx.student.account = edx.student.account || {}; + + edx.student.account.RegisterModel = Backbone.Model.extend({ + + defaults: { + email: '', + name: '', + username: '', + password: '', + level_of_education: '', + gender: '', + year_of_birth: '', + mailing_address: '', + goals: '', + termsofservice: false + }, + + urlRoot: '', + + initialize: function( obj ) { + this.urlRoot = obj.url; + }, + + sync: function(method, model) { + var headers = { + 'X-CSRFToken': $.cookie('csrftoken') + }; + + $.ajax({ + url: model.urlRoot, + type: 'POST', + data: model.attributes, + headers: headers + }) + .done(function() { + var query = window.location.search, + url = '/dashboard'; + + // model.trigger('sync'); + + // If query string in url go back to that page + if ( query.length > 1 ) { + url = query.substring( query.indexOf('=') + 1 ); + } + + window.location.href = url; + }) + .fail( function( error ) { + console.log('RegisterModel.save() FAILURE!!!!!'); + model.trigger('error', error); + }); + } + }); +})(jQuery, _, Backbone, gettext); \ No newline at end of file diff --git a/lms/static/js/student_account/views/AccessView.js b/lms/static/js/student_account/views/AccessView.js index 432552fbba..40486b3fc4 100644 --- a/lms/static/js/student_account/views/AccessView.js +++ b/lms/static/js/student_account/views/AccessView.js @@ -44,6 +44,9 @@ var edx = edx || {}; if ( type === 'login' ) { console.log('load login'); return new edx.student.account.LoginView(); + } else if ( type === 'register' ) { + console.log('load register'); + return new edx.student.account.RegisterView(); } // return new app.LoginView({ diff --git a/lms/static/js/student_account/views/LoginView.js b/lms/static/js/student_account/views/LoginView.js index 10fd470566..1e26bf8727 100644 --- a/lms/static/js/student_account/views/LoginView.js +++ b/lms/static/js/student_account/views/LoginView.js @@ -42,8 +42,10 @@ var edx = edx || {}; }, postRender: function() { + var $container = $(this.el); - this.$form = $(this.el).find('form'); + this.$form = $container.find('form'); + this.$errors = $container.find('.error-msg'); }, getInitialData: function() { @@ -81,10 +83,6 @@ var edx = edx || {}; fieldTpl = this.fieldTpl; for ( i=0; ichecked<% } %> > -
+
diff --git a/lms/templates/student_account/form_field.underscore b/lms/templates/student_account/form_field.underscore index b5787baf5a..13dd53d787 100644 --- a/lms/templates/student_account/form_field.underscore +++ b/lms/templates/student_account/form_field.underscore @@ -10,11 +10,25 @@ Forgot password? <% } %> - minlength="<%= restrictions.min_length %>"<% } %> - <% if ( restrictions.max_length ) { %> maxlength="<%= restrictions.max_length %>"<% } %> - <% if ( required ) { %> required<% } %> - /> + <% if ( type === 'select' ) { %> + + <% } else if ( type === 'textarea' ) { %> + + <% } else { %> + minlength="<%= restrictions.min_length %>"<% } %> + <% if ( restrictions.max_length ) { %> maxlength="<%= restrictions.max_length %>"<% } %> + <% if ( required ) { %> required<% } %> + /> + <% } %> <% if ( type === 'checkbox' ) { %>