I have an Angular page with a form for adding people and a button (outside this form) to send a list of people.
When the user focuses on the text input in the form, and then clicks to submit the list, a validation error with text input appears, but the submit event never occurs.
An example of this problem is here: http://plnkr.co/edit/6Z0UUs
<div ng-controller="MyCtrl as vm"> <form name="form1" novalidate=""> <input type="text" name="field1" ng-model="vm.name" required> <div ng-messages="form1.field1.$error" ng-if="form1.field1.$touched"> <label ng-message="required">Name is required</label> </div> </form> <button ng-click="vm.submit()">Submit list</button> </div>
-
angular.module('myApp',[]) .controller('MyCtrl', function() { var vm = this; vm.name = ''; vm.submit = function () { alert('submitted'); }; });
Repeat:
- Click on text entry but leave blank
- Click submit
Current behavior: "Name required" appears due to validation. Pressing "Submit" again displays a warning "sent".
Expected behavior: "Name required" appears after verification, and a "sent" warning appears.
Desired behavior: a “sent” warning appears, and I add some code to vm.submit () to hide the “Name required” confirmation message, since it is not important when sending the list.
I noticed that removing the ng-messages block fixes the problem, but I need to show a validation message. Using a more basic directive ( ng-show ) to display a validation message does not help.
Any understanding of what I am doing wrong, or workarounds to achieve the desired behavior, would be great!
source share