I am unable to find a solution in the existing answers, hence i am posting this.
I have a form which has many input fields, many of them are required.
There are buttons (more than 2) in the form and are tied to functions in controllers using ng-click.
I need to have form validated on ng-click before the function is executed.
By default, form validation is happening after function execution. Function should not run if required fields are not filled.
I have created a fiddle. https://jsfiddle.net/z1uyyqg9/
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.name=undefined;
$scope.preview = function(){
alert("Previewed");
};
$scope.update = function(){
alert("Updated");
}
});
</script>
<div ng-app="myApp" ng-controller="myCtrl">
<form>
<input type="text" ng-model='name' required>
<button ng-click='preview()'>Preview</button>
<button ng-click='update()'>Update</button>
</form>
</div>
ng-disabled="myFormName.$invalid"to your buttons.