Last active
May 11, 2017 10:28
-
-
Save victorjonsson/dc21cb51a67ba580a6490d94d5abecfe to your computer and use it in GitHub Desktop.
statement-validator.js
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
$.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