3

Is there any way how to trigger validation when using ngModelOptions? My use-case is to have a form with all fields updating the ngModel on submit (due to the fact that I would like to revert the whole form when user clicks on Cancel button). Having this, I cannot validate my fields instantly. Fields are validated just when the model is updated thus onSubmit. Is there any build in solution or should I use my custom implementation?

<form name="editForm" ng-submit="edit()">
  <input type="text" name="text" required maxlength="140" ng-model="myObject.text"
         ng-model-options="{ updateOn: 'submit' }" />

  <button type="submit" ng-disabled="editForm.$invalid">
    Save
  </button>
</form>
kuceram
  • 3,795
  • 9
  • 34
  • 54

1 Answers1

0

What I usually do is commit the form's view value on the js code and if form is invalid then do not submit. Thus in HTML I add an on-click attribute like:

<button on-click="edit(editForm)">

And then on the javascript

 edit = function(editForm) {
   editForm.$commitViewValue;
   if (editForm.$valid) { // submit code }
 }
goulpan
  • 466
  • 4
  • 6