javascript - knockout validating multiple levels deep observable array -
hi need create custom validator aplyed each element of observable array using knockout validation plugin. structure of object when post server:
var viewmodel = { evaluationformdatacontract: { studentassignmentinstanceid: value, evaluationtype: value, categories: array[ categoriesonevaluationdatacontract1 = { memo: value, categoryid: value, title: value, // fields needed validation hasmemo: value, memoismandatory: value questions: array[ questionsonevalcategorydatacontract1 = { memo: value, grade: value, hasgrade: value, hasmemo: value, showonlymemo: value }, questionsonevalcategorydatacontract2 = { memo: value, grade: value, hasgrade: value, hasmemo: value, showonlymemo: value }] }, categoriesonevaluationdatacontract2 = { memo: value, categoryid: value, title: value, // fields needed validation hasmemo: value, memoismandatory: value questions: array[ questionsonevalcategorydatacontract1 = { memo: value, grade: value, hasgrade: value, hasmemo: value, showonlymemo: value }, questionsonevalcategorydatacontract2 = { memo: value, grade: value, hasgrade: value, hasmemo: value, showonlymemo: value }, questionsonevalcategorydatacontract3 = { memo: value, grade: value, hasgrade: value, hasmemo: value, showonlymemo: value }] }, ] }
}
now validation have applyed on 2 nested arrays , done based on properties.
the first validation has done on each object of categories array , check if hasmemo , memoismandatory if case memo required.
the second validation done on each object of questions array , check if hasgrade if case grade required.
the last validation done on hasmemo , showonlymemo , used memo value on questions object.
reading documentation validation plugin found how extend simple observable .witch seems done this:
ko.validation.rules['mustequal'] = { validator: function (val, otherval) { return val === otherval; }, message: 'the field must equal {0}' };
but not think work structure of viwmodel.how can create validators each observable in observablearrays?
first off, agree tomalak. rather posting bunch of nonsense code "looks like", should post actual code readable. instance, can't tell if using observable
, computed
or observablearray
members, have assume observable
or observablearray
, there no computed
members.
now, said:
the first validation has done on each object of categories array , check if hasmemo , memoismandatory if case memo required.
let me naming property hasmemo
, mean memo
field required terrible! if call hasmemo
, should mean thing in question has memo. , why need @ both hasmemo
, memoismandatory
see if memo
required? ditto hasgrade
.
regardless, need add validation each of observables
on classes. wait, that's assumption. using classes, right? you're not creating single object , giving bunch of nested arrays , objects without using constructors, you? i'll proceed assuming you're creating constructors , leave @ that.
i'll focus on first validation because second 1 , third 1 unintelligible me. let's "categoriesonevaluationdatacontract1" object uses following constructor:
function category() { var self = this; self.categoryid = ko.observable(); self.hasmemo = ko.observable(); self.memoismandatory = ko.observable(); self.memo = ko.observable(); //etc etc... }
you need extend memo
validator, in case want required validator. this:
self.memo = ko.observable().extend({ required: true });
this makes memo
required. not want, want required when hasmemo
, memoismandatory
both true, right?. need do:
self.memo = ko.observable().extend({ required: { onlyif: function() { return self.hasmemo() && self.memoismandatory(); } } });
there. that's there it. should able figure out rest. , if not, let me know. :)
Comments
Post a Comment