Created
September 22, 2020 15:24
-
-
Save smailliwcs/3bcdcf4974188591d681bf0513e7c141 to your computer and use it in GitHub Desktop.
Knockout binding handler: validationPopover
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
<script type="text/javascript"> | |
ko.bindingHandlers.validationPopover = { | |
init: function (element, valueAccessor, allBindings) { | |
if (!allBindings.has("value")) { | |
return; | |
} | |
var $element = $(element); | |
var value = allBindings.get("value"); | |
$element.popover($.extend({}, ko.unwrap(valueAccessor()), { | |
content: function () { | |
return value.error(); | |
}, | |
trigger: "manual" | |
})); | |
value.subscribe(function () { | |
$element.popover(value.isValid() ? "hide" : "show"); | |
}); | |
} | |
}; | |
</script> | |
<input type="text" data-bind="value: name, validationPopover: {}" /> | |
<script type="text/javascript"> | |
ko.validation.init({ | |
insertMessages: false | |
}); | |
function ViewModel() { | |
var self = this; | |
self.name = ko.observable().extend({ required: true }); | |
} | |
ko.applyBindings(new ViewModel()); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment