JSFiddle - React, Tailwind, and code Playground
HTML
<script src="http://code.jquery.com/jquery-1.9.0.js"></script>
<script src="https://rawgithub.com/ericmbarnard/Knockout-Validation/master/Src/knockout.validation.js"></script>
<div data-bind="foreach:PhoneList">
Country Code:
<input type="text" data-bind="value:Code" />
<br/>
country:
<select data-bind="options:$root.countyList,optionsCaption:'--select--',optionsText:'Text',optionsValue:'Value',value:$data.country" />
<br/>
<input type="button" id="btnRemove"
value="Remove" data-bind="click: $parent.remove" />
</div>
<div>
<input type="button" id="btnAdd"
value="Add Another" data-bind="click: add" />
<input type="submit" data-bind="click: save" />
</div>
CSS
.validationMessage{
color: red;
}
JavaScript
function Phone() {
var self = this;
self.Code = ko.observable("");
self.country = ko.observable("");
self.Code.subscribe(function () { alert("Code has changed"); });
self.country.subscribe(function () { alert("Country has changed"); });
self.Validation = ko.validatedObservable([
self.Code.extend({ required: true }),
self.country.extend({ required: true }),
]);
}
function PhoneViewModel() {
var self = this;
self.PhoneList = ko.observableArray([new Phone()]);
self.remove = function () {
self.PhoneList.remove(this);
};
self.add = function () {
self.PhoneList.push(new Phone());
};
self.countyList = [
{ Text: 'india', Value: 1 },
{ Text: 'usa', Value: 2 },
{ Text: 'uk', Value: 3 },
{ Text: 'brazil', Value: 4 }
];
self.save = function () {
var errors = 0;
ko.utils.arrayForEach(self.PhoneList(), function (item) {
if (item.Validation.errors().length != 0) {
item.Validation.errors.showAllMessages();
errors++;
}
});
if(errors) {
alert("has errors");
}
else {
alert("all good");
}
}
}
ko.applyBindings(new PhoneViewModel());
$(document).ready(function () {
});