Commit 2284d5ee by AlasdairSwan

Merge pull request #5713 from edx/alasdair/logistration-validation

ECOM-466 cleaned up form submission validation
parents 56d55608 c3083069
...@@ -10,29 +10,40 @@ var edx = edx || {}; ...@@ -10,29 +10,40 @@ var edx = edx || {};
validate: { validate: {
msg: { msg: {
email: '<p>A properly formatted e-mail is required</p>', email: '<li>A properly formatted e-mail is required</li>',
min: '<p><%= field %> must be a minimum of <%= length %> characters long', min: '<li><%= field %> must be a minimum of <%= count %> characters long</li>',
max: '<p><%= field %> must be a maximum of <%= length %> characters long', max: '<li><%= field %> must be a maximum of <%= count %> characters long</li>',
password: '<p>A valid password is required</p>', password: '<li>A valid password is required</li>',
required: '<p><%= field %> field is required</p>', required: '<li><%= field %> field is required</li>',
terms: '<p>To enroll you must agree to the <a href="#">Terms of Service and Honor Code</a></p>' terms: '<li>To enroll you must agree to the <a href="#">Terms of Service and Honor Code</a></li>'
}, },
field: function( el ) { field: function( el ) {
var $el = $(el), var $el = $(el),
required = _fn.validate.required( $el ), required = true,
// length = _fn.validate.charLength( $el ), min = true,
min = _fn.validate.str.minlength( $el ), max = true,
max = _fn.validate.str.maxlength( $el ), email = true,
email = _fn.validate.email.valid( $el ), response = {},
response = { isBlank = _fn.validate.isBlank( $el );
isValid: required && min && max && email
}; if ( _fn.validate.isRequired( $el ) ) {
if ( isBlank ) {
required = false;
} else {
min = _fn.validate.str.minlength( $el );
max = _fn.validate.str.maxlength( $el );
email = _fn.validate.email.valid( $el );
}
} else if ( !isBlank ) {
email = _fn.validate.email.valid( $el );
}
response.isValid = required && min && max && email;
if ( !response.isValid ) { if ( !response.isValid ) {
response.message = _fn.validate.getMessage( $el, { response.message = _fn.validate.getMessage( $el, {
required: required, required: required,
// length: length,
min: min, min: min,
max: max, max: max,
email: email email: email
...@@ -42,28 +53,6 @@ var edx = edx || {}; ...@@ -42,28 +53,6 @@ var edx = edx || {};
return response; return response;
}, },
charLength: function( $el ) {
var type = $el.attr("type");
if (type !== "text" && type !== "textarea" && type !== "password") {
return true;
}
// 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;
},
str: { str: {
minlength: function( $el ) { minlength: function( $el ) {
var min = $el.attr('minlength') || 0; var min = $el.attr('minlength') || 0;
...@@ -84,12 +73,12 @@ var edx = edx || {}; ...@@ -84,12 +73,12 @@ var edx = edx || {};
} }
}, },
required: function( $el ) { isRequired: function( $el ) {
if ( $el.attr('type') === 'checkbox' ) { return $el.attr('required');
return $el.attr('required') ? $el.prop('checked') : true; },
} else {
return $el.attr('required') ? $el.val() : true; isBlank: function( $el ) {
} return ( $el.attr('type') === 'checkbox' ) ? $el.prop('checked') : !$el.val();
}, },
email: { email: {
...@@ -104,7 +93,7 @@ var edx = edx || {}; ...@@ -104,7 +93,7 @@ var edx = edx || {};
), ),
valid: function( $el ) { valid: function( $el ) {
return $el.data('email') ? _fn.validate.email.format( $el.val() ) : true; return $el.attr('type') === 'email' ? _fn.validate.email.format( $el.val() ) : true;
}, },
format: function( str ) { format: function( str ) {
...@@ -115,7 +104,8 @@ var edx = edx || {}; ...@@ -115,7 +104,8 @@ var edx = edx || {};
getMessage: function( $el, tests ) { getMessage: function( $el, tests ) {
var txt = [], var txt = [],
tpl, tpl,
name; name,
obj;
_.each( tests, function( value, key ) { _.each( tests, function( value, key ) {
if ( !value ) { if ( !value ) {
...@@ -123,9 +113,17 @@ var edx = edx || {}; ...@@ -123,9 +113,17 @@ var edx = edx || {};
tpl = _fn.validate.msg[key]; tpl = _fn.validate.msg[key];
txt.push( _.template( tpl, { obj = {
field: _fn.validate.str.capitalizeFirstLetter( name ) field: _fn.validate.str.capitalizeFirstLetter( name )
})); };
if ( key === 'min' ) {
obj.count = $el.attr('minlength');
} else if ( key === 'max' ) {
obj.count = $el.attr('maxlength');
}
txt.push( _.template( tpl, obj ) );
} }
}); });
......
...@@ -15,6 +15,8 @@ var edx = edx || {}; ...@@ -15,6 +15,8 @@ var edx = edx || {};
'click .js-reset': 'submitForm' 'click .js-reset': 'submitForm'
}, },
formType: 'password-reset',
requiredStr: '', requiredStr: '',
postRender: function() { postRender: function() {
......
...@@ -16,6 +16,8 @@ var edx = edx || {}; ...@@ -16,6 +16,8 @@ var edx = edx || {};
'click .login-provider': 'thirdPartyAuth' 'click .login-provider': 'thirdPartyAuth'
}, },
formType: 'register',
initialize: function( data ) { initialize: function( data ) {
this.tpl = $(this.tpl).html(); this.tpl = $(this.tpl).html();
this.fieldTpl = $(this.fieldTpl).html(); this.fieldTpl = $(this.fieldTpl).html();
......
...@@ -585,7 +585,7 @@ ...@@ -585,7 +585,7 @@
list-style: none; list-style: none;
li { li {
margin: 0 0 ($baseline/4) 0; margin: 0; /*0 0 ($baseline/4) 0;*/
} }
} }
} }
...@@ -598,10 +598,6 @@ ...@@ -598,10 +598,6 @@
.message-title { .message-title {
color: shade($red, 10%) !important; color: shade($red, 10%) !important;
} }
.message-copy {
}
} }
// misc // misc
......
<form id="login"> <form id="login">
<div class="status submission-error hidden" aria-hidden="true"> <div class="status submission-error hidden" aria-hidden="true">
<h4 class="message-title">We couldn't log you in.</h4> <h4 class="message-title">We couldn't log you in.</h4>
<div class="message-copy"> <ul class="message-copy"></ul>
<p>Email or password is incorrent. <a href="#">Forgot password?</a></p>
</div>
</div> </div>
<div class="already-authenticated-msg hidden"> <div class="already-authenticated-msg hidden">
<% if (currentProvider) { %> <% if (currentProvider) { %>
......
...@@ -8,9 +8,7 @@ ...@@ -8,9 +8,7 @@
<form id="password-reset-form"> <form id="password-reset-form">
<div class="status submission-error hidden" aria-hidden="true"> <div class="status submission-error hidden" aria-hidden="true">
<h4 class="message-title">An error occurred.</h4> <h4 class="message-title">An error occurred.</h4>
<div class="message-copy"> <ul class="message-copy"></ul>
<p>Please provide a valid email.</p>
</div>
</div> </div>
<%= fields %> <%= fields %>
......
...@@ -14,9 +14,7 @@ ...@@ -14,9 +14,7 @@
<form id="register" autocomplete="off"> <form id="register" autocomplete="off">
<div class="status submission-error hidden" aria-hidden="true"> <div class="status submission-error hidden" aria-hidden="true">
<h4 class="message-title">An error occurred in your registration.</h4> <h4 class="message-title">An error occurred in your registration.</h4>
<div class="message-copy"> <ul class="message-copy"></ul>
<p>Please provide a valid email.</p>
</div>
</div> </div>
<%= fields %> <%= fields %>
<button class="action action-primary action-update js-register">Create My edX Account</button> <button class="action action-primary action-update js-register">Create My edX Account</button>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment