Hi I need to create a custom validator that will be aplyed for each element of an observable array using knockout validation plugin. The structure of my object will look something like this when I post it to the server:
var viewModel = {
evaluationFormDataContract: {
studentAssignmentInstanceId: value,
evaluationType: value,
categories: array[
CategoriesOnEvaluationDataContract1 = {
memo: value,
categoryId: value,
title: value,
// Fields needed for 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 for 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 the validation will have to be applyed only on the two nested arrays and will be done based on some properties.
The first validation has to be done on each object of the categories array and it will check if hasMemo and memoIsMandatory if this is the case memo will be required.
The second validation will be done on each object of questions array and it will check if hasGrade if that is the case grade will be required.
The last validation will be done on hasMemo and showOnlyMemo and are will be used for the memo value on the questions object.
Reading the documentation for the validation plugin I found how I would extend a simple observable .Witch it seems to be done something like this:
ko.validation.rules['mustEqual'] = {
validator: function (val, otherVal) {
return val === otherVal;
},
message: 'The field must equal {0}'
};
But I do not think this will work for the structure of my viwmodel.How can I create validators for each observable in my observableArrays?