Skip to content

Instantly share code, notes, and snippets.

@smailliwcs
Created September 22, 2020 15:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smailliwcs/3bcdcf4974188591d681bf0513e7c141 to your computer and use it in GitHub Desktop.
Save smailliwcs/3bcdcf4974188591d681bf0513e7c141 to your computer and use it in GitHub Desktop.
Knockout binding handler: validationPopover
<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