Last active
July 2, 2016 11:26
-
-
Save nubz/9810969 to your computer and use it in GitHub Desktop.
Backbone: Live Form Validation
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
_.extend(Backbone.View.prototype, { | |
checkInput: function (e, target, submitBtn) { | |
var _this = this; | |
var error = 0; | |
submitBtn = submitBtn || '.submit'; | |
$('span.error').remove(); | |
$('.error').removeClass('error'); | |
_.each(this.fields, function (field, i, list) { | |
var $el = $(field.$el); | |
field.default = field.default || ''; | |
var curval; | |
switch (field.type) { | |
case 'checkbox': | |
curval = ($el[0].checked) ? 1 : ''; | |
break; | |
case 'text-container': | |
curval = $el.text(); | |
break; | |
case 'attribute': | |
curval = $el.attr(field.attr); | |
break; | |
case 'text': | |
case 'textarea': | |
case 'html': | |
case 'radio': | |
case 'number': | |
case 'password': | |
default: | |
curval = $el.val(); | |
break; | |
} | |
var pass = true; | |
// mirrors are text elements that mirror the content of this field | |
if (field.mirrors) { | |
_.each(field.mirrors, function (mirror, i) { | |
$('#' + mirror).text(curval); | |
}); | |
} | |
// computes is a callback to be executed with the current value | |
if (field.computes) { | |
this[field.computes](curval); | |
} | |
// requires further values - must be an array | |
if (field.requires && curval !== undefined && curval !== field.default) { | |
_.each(field.requires, function (field) { | |
_this.fields[field].required = true; | |
}); | |
} else if (field.requires) { | |
_.each(field.requires, function (field) { | |
_this.fields[field].required = false; | |
}); | |
} | |
// specifically required to be checked online for validity | |
if (field['check-online']) { | |
var checkpass = function (val) { | |
pass = val; | |
}; | |
field.pass(curval, checkpass); | |
} else { | |
// console.log('value to check: ' + curval); | |
pass = field.pass(curval); | |
} | |
//console.log('checking field ' + field.label + ', required: ' + field.required + ' which returns ' + pass); | |
if (field.required && !pass) { | |
error++; | |
var target_id = (e) ? $(e.target).attr('id') : target.attr('id'); | |
if (target_id && target_id === $el.attr('id')) { | |
if (field.suppress_errors) { | |
$el.addClass('error'); | |
} else { | |
$el.after('<span class="error">' + (field.error || field.label + ' is required') + '</span>'); | |
} | |
} | |
} else if (!pass && curval !== '') { | |
error++; | |
if (e && $(e.target).attr('id') === $el.attr('id')) { | |
$el.addClass('error').after('<span class="error">' + (field.error || field.label + ' is required') + '</span>'); | |
} | |
} else { | |
$el.removeClass('error'); | |
} | |
}, this); | |
if (error === 0) { | |
$('span.error').remove(); | |
$(submitBtn).attr('disabled', false); | |
} else { | |
$(submitBtn).attr('disabled', 'disabled'); | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment