You can do:
<form name="loginform" novalidate ng-submit="loginform.$valid && login.submit()">
No need for controller checks.
Change the submit button to:
<button type="submit" ng-disabled="loginform.$invalid">Login</button>
So the suggested answer from TheHippo did not work for me, instead I ended up sending the form as a parameter to the function like so:
<form name="loginform" novalidate ng-submit="login.submit(loginForm)" class="css-form">
This makes the form available in the controller method:
$scope.login = {
submit : function(form) {
if(form.$valid)....
}
Your forms are automatically put into $scope as an object. It can be accessed via $scope[formName]
Below is an example that will work with your original setup and without having to pass the form itself as a parameter in ng-submit.
var controller = function($scope) {
$scope.login = {
submit: function() {
if($scope.loginform.$invalid) return false;
}
}
};
Working example: http://plnkr.co/edit/BEWnrP?p=preview
HTML:
<div class="control-group">
<input class="btn" type="submit" value="Log in" ng-click="login.onSubmit($event)">
</div>
In your controller:
$scope.login = {
onSubmit: function(event) {
if (dataIsntValid) {
displayErrors();
event.preventDefault();
}
else {
submitData();
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With