Skip to content

Instantly share code, notes, and snippets.

@victorjonsson
Last active May 11, 2017 10:28
Show Gist options
  • Save victorjonsson/dc21cb51a67ba580a6490d94d5abecfe to your computer and use it in GitHub Desktop.
Save victorjonsson/dc21cb51a67ba580a6490d94d5abecfe to your computer and use it in GitHub Desktop.
statement-validator.js
$.formUtils.addValidator({
name: 'statement',
validatorFunction : function(value, $elem, conf, language, $form, eventContext) {
var statementIsValid = true,
$resultTree = $('<result><node></node></result>'),
$current = $resultTree.children().eq(0),
invokeNext = true;
$.each(value.replace(/\s+/g,' ').split(' '), function (i, str) {
if (str == '||') {
invokeNext = true;
} else if (invokeNext) {
invokeNext = false;
$current.append('<span>'+invokeValidator(str, value, $elem, conf, language, $form, eventContext)+'</span>');
} else if (str == '&&') {
var $newNode = $('<node></node>');
$current.parent().append($newNode);
$current = $newNode;
}
});
$resultTree.children().each(function () {
var nodeHasAtleastOneValid = false;
$(this).children().each(function () {
if ($(this).text() == 'true') {
nodeHasAtleastOneValid = true;
return false;
}
});
if (!nodeHasAtleastOneValid) {
statementIsValid = false;
return false;
}
});
return statementIsValid;
},
errorMessage: '',
errorMessageKey: 'badTime'
});
function invokeValidator(validator, value, $elem, conf, language, $form, eventContext) {
return $.formUtils.validators['validate_'+validator].validatorFunction(value, $elem, conf, language, $form, eventContext);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment