Last active
August 29, 2015 14:18
-
-
Save hughbris/2c38d3b2652444fc72c2 to your computer and use it in GitHub Desktop.
bbsSubmit
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
/* | |
Imagine a world where Javascript is relied upon to submit a form, the only means of validation. That is currently my sad reality. | |
Needs are basic for this instance. It's a temporary state of affairs and This Will Do Now (TM). | |
*/ | |
function bbsSubmit(action) { | |
$("[class~='error'][class~='field']").each( function () { | |
$(this).removeClass('error'); | |
} ); | |
var errors = testRequiredFields(); | |
if(errors) { | |
var msg = "Please correct these problems before continuing:\n\n"; | |
var focused = false; | |
for(e in errors) { | |
msg += "\t* " + errors[e] + "\n"; | |
if (!focused) { | |
$('#' + e).focus(); | |
focused = true; | |
} | |
} | |
window.alert($.trim(msg)); | |
} | |
else { | |
// theForm is a magical global defined inline, the only form element in the DOM so gotta use it | |
theForm.encoding = 'application/x-www-form-urlencoded'; | |
theForm.action = action; | |
theForm.submit(); | |
} | |
} | |
function testRequiredFields() { | |
var errors = new Object(); | |
var requiredElements = $("[class~='field'][class~='required']").each( function() { | |
var field = $(this); | |
$(this).find('.control').each( function() { | |
var empty = !($.trim($(this).val())); | |
if (empty) { | |
field.addClass('error'); | |
errors[this.id] = 'The ' + deriveFieldDescription(field) + " field must be provided.\n"; | |
} | |
} ); | |
} ); | |
// special bit for a very special control instance - need to make sure this isn't classed as a control | |
// avert your eyes | |
nameOfTheBeast = 'rdoProvider_REQ'; | |
if (!($("input:radio[name='" + nameOfTheBeast + "']:checked").val())) { | |
$("input:radio[name='" + nameOfTheBeast + "']").parent(".field.radios").addClass('error'); | |
errors[nameOfTheBeast] = "Please select a provider.\n"; | |
} | |
return errors; | |
} | |
function deriveFieldDescription(element) { | |
//FIXME: it's not this simple. Update: the conditionals are one idea, started | |
if (element.hasClass('radios')) { | |
console.log('radios..'); return ('foo'); | |
} | |
else { | |
return element.find('label').text().replace(/[^\w\s,-]/g,'' ); | |
} | |
} | |
/* | |
.field.error label { | |
color: red; | |
} | |
.field.error .control { | |
border: solid red; | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment