KnockoutJs BindingHandlers cancels validation

105 Views Asked by At

i'm using knockout.js and adding a BindingHandlers to format an observable to decimal. The thing is that when i applied this binding the required validationmessage is not appearing.

This is the code

function formatWithComma(x, precision, seperator) {
        var options = {
            precision: precision || 2,
            seperator: seperator || ','
        }
        var formatted = parseFloat(x, 10).toFixed(options.precision);
        var regex = new RegExp(
                '^(\\d+)[^\\d](\\d{' + options.precision + '})$');
        formatted = formatted.replace(
            regex, '$1' + options.seperator + '$2');
        return formatted;
    }

    function reverseFormat(x, precision, seperator) {
        var options = {
            precision: precision || 2,
            seperator: seperator || ','
        }
        var regex = new RegExp(
            '^(\\d+)[^\\d](\\d+)$');
        var formatted = x.replace(regex, '$1.$2');
        return parseFloat(formatted);
    }

    ko.bindingHandlers.commaDecimalFormatter = {
            init: function (element, valueAccessor) {
                var observable = valueAccessor();

                var interceptor = ko.computed({
                    read: function () {
                        if (!observable())
                            return 0;
                        else
                            return formatWithComma(observable());
                    },
                    write: function (newValue) {
                        observable(reverseFormat(newValue));
                    }
                });

                if (element.tagName == 'INPUT')
                    ko.applyBindingsToNode(element, {
                        value: interceptor
                    });
                else
                    ko.applyBindingsToNode(element, {
                        text: interceptor
                    });
            },
            update: function (element, valueAccessor) {

                var observable = valueAccessor();

                var interceptor = ko.computed({
                    read: function () {
                        if (!observable())
                            return 0;
                        else
                            return formatWithComma(observable());
                    },
                    write: function (newValue) {
                        observable(reverseFormat(newValue));
                    }
                });          
            }
        }
        ko.validation.makeBindingHandlerValidatable('commaDecimalFormatter'); 

--The observable

self.Number = ko.observable(0).extend({
    required: {
        params: true,
        message: "Requerido"
    }
});

--- and the input

<input id="txtGroam" data-bind="commaDecimalFormatter: Model().Number, valueUpdate: 'keyup'" />
<p class="text-danger-alt" data-bind="validationMessage: Model().Number"></p>

If I use 'value' instead of 'commaDecimalFormatter', the validation works perfectly.

Any clues?

0

There are 0 best solutions below