Skip to content

Instantly share code, notes, and snippets.

@jeffijoe
Last active August 29, 2015 14:06
Show Gist options
  • Save jeffijoe/20d4cbb4f531fc471377 to your computer and use it in GitHub Desktop.
Save jeffijoe/20d4cbb4f531fc471377 to your computer and use it in GitHub Desktop.
jQuery.Validator onFieldValidatedHandler
var setValidationStatusFor = function($field, isValid, errorMessage) {
var
$formgroup = $field.closest(".form-group"),
$helpBlock = $formgroup.find(".help-block");
if (isValid) {
if ($helpBlock.data().hasShownErrorBefore) {
$helpBlock.text($helpBlock.data().originalText);
}
$formgroup.removeClass("has-error");
} else {
if (!$helpBlock.data().hasShownErrorBefore) {
$helpBlock.data().hasShownErrorBefore = true;
$helpBlock.data().originalText = $helpBlock.text();
}
$helpBlock.text(errorMessage);
$formgroup.addClass("has-error");
}
};
var handleFieldValidationResult = function($element, isValid, result) {
var errorMessage = result.messages[0];
setValidationStatusFor($element, isValid, errorMessage);
};
var validatorCfg = {
useInlineErrors: true,
onFieldValidated: handleFieldValidationResult
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment