knockout jsclear validation errors on modal

101 Views Asked by At

I have a view which has an "add" button on button click event I have a new modal with some input elements. I am trying to add knockout validation to these modal input elements. The validation works the first time but when i close the dialog and open the dialog again the previous validations exist.and everytime errors keep appending.how to clear validation errors on modal close?

ko.validation.init({
 errorElementClass: 'error-msg',
 decorateElement:true
}, true);

var viewModel = {
 LastName: ko.observable('').extend({
  required: { message: 'Last Name is required.' }
 }),
 FirstName: ko.observable('').extend({
  required: { message: 'First Name is required.' }
 }),
};

viewModel.addUpdatDoctor = function () {
 if (updateDoctorModel.errors().length === 0) {
  $.ajax({
   type: "POST",

  });
 }
 else {
  viewModel.errors.showAllMessages();
 }
};
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout-validation/1.0.2/knockout.validation.min.js"></script>
<div class="bodyItem">
 <input  type="text" data-bind="value: FirstName"  />
 <label >First Name:</label>
 <span class="error-msg" data-bind="validationElement: FirstName "></span>
</div>

<div class="bodyItem">
 <input type="text" data-bind="value: LastName"  />
 <label>Last Name:</label>
 <span class="error-msg" data-bind="validationElement: LastName"></span>
</div>

0

There are 0 best solutions below