cuterthanbacon
cuterthanbacon

Reputation: 1

Knockout js validation plugin issues when validating an observableArray of observables

I have an observable array of observables - each observable has been extended for require however when i save the form with cleared fields it does not collect error or have any errors to show -

ko.validation.configure({
    grouping: {
        deep: true,
        observable: true
    }
});


var viewModel = function () {
    var self = this;
    var data = {
        "deadzone": "0.2",
            "sloperange": "2",
            "stepsize": "0.8",
            "setpoint": "0.75",
            "pressure": "1"
    };
    self.defaultCalculations = ko.observableArray([]);
    self.defaultCalculationProperties = function (defaults) {
        var self = this;
        var properties = defaults;
        self.deadzone = ko.observable(properties.deadzone || '').extend({
            required: {
                message: '*required'
            }
        });
        self.sloperange = ko.observable(properties.sloperange || '').extend({
            required: {
                message: '*required'
            }
        });
        self.stepsize = ko.observable(properties.stepsize || '').extend({
            required: {
                message: '*required'
            }
        });
        self.setpoint = ko.observable(properties.setpoint || '').extend({
            required: {
                message: '*required'
            }
        });
        self.pressure = ko.observable(properties.pressure || '').extend({
            required: {
                message: '*required'
            }
        });

    };
    //populate calculation values
    self.populateCalculationDefaults = function (data) {

        //pushing data to an array and mapping each item
        var temp = [];
        temp[0] = data;
        var mappedTasks = _.map(temp, function (item) {
            return new self.defaultCalculationProperties(item);
        });

        self.defaultCalculations(mappedTasks);
    };

    self.save = function (model) {
        //console.log(model);
        console.log(self.errors().length, self.errors.showAllMessages());
        if (self.errors().length != 0) {
            self.errors.showAllMessages();
        }
    };
    self.errors = ko.validation.group(self.defaultCalculations());

    self.populateCalculationDefaults(data);
};

ko.applyBindings(new viewModel());

Not sure what the problem is and have scoured over stackflow - any suggestions?

http://jsfiddle.net/kmcadams/W2SZb/

Upvotes: 0

Views: 1759

Answers (2)

Mik
Mik

Reputation: 4434

You can also add notify always when extending observable.

this.validationModel = ko.validatedObservable({
    Num: ko.observable(this.Num).extend({ number: true, notify: 'always' }),
});

It worked for me. Before, when this.Num wasn't modified, it wasn't validated.

Upvotes: 0

Kevin Nacios
Kevin Nacios

Reputation: 2853

Easiest thing to do is call ko.validation.group again inside the populate function. Another option, which i prefer, is to subscribe to defaultcalculations and run the group call in there.

self.defaultCalculations.subscribe(function () {
    self.errors = ko.validation.group(self.defaultCalculations);
});

this will manually subscribe the function to be run anytime defaultCalculations has an item added/removed.

Upvotes: 1

Related Questions