I have some trouble combining client side validation and server side validation in AngularJS. Here's the HTML:
<form class="form-login" name="loginForm" novalidate ng-submit="login()">
<fieldset>
<h2>Login</h2>
<input type="text" required class="input-block-level" placeholder="Username" ng-model="username" name="username">
<div ng-show="loginForm.username.$dirty && loginForm.username.$invalid">
<span ng-show="loginForm.username.$error.required">Username required.</span>
</div>
<input type="password" required class="input-block-level" placeholder="Password" ng-model="password" name="password">
<div ng-show="loginForm.password.$dirty && loginForm.password.$invalid">
<span ng-show="loginForm.password.$error.required">Password required.</span>
</div>
<div ng-show="loginForm.username.$dirty && loginForm.password.$dirty && loginForm.$invalid">
<span>Invalid username or password</span>
</div>
<p><a href="#forgotPassword" data-toggle="modal" class="btn btn-link">Forgot password?</a></p>
<p><button class="btn btn-primary btn-large" ng-disabled="loginForm.$invalid" type="submit">Login</button></p>
</fieldset>
</form>
...and here's the login function:
$scope.login = function() {
$http.post('url', {
"password": $scope.password
}).success(function(data, status, headers, config) {
console.log(status);
console.log(data);
}).error(function(data, status, headers, config) {
if (404 === status) {
$scope.loginForm.$invalid = true;
}
});
return false;
}
The client side validation works fine until I submit the form and get back a 404 status. When the 404 status is returned the loginForm.$invalid error shows up but the client side validation doesn't change the form state back to valid when I change the username and password inputs again.
Is there a standard way to do this kind of client side + server side validation in the same form?