简体   繁体   中英

AngularJS server and client side form validation

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?

I don't think you're supposed to modify form $invalid or $valid directly. You should be doing it via $setValidity(validationErrorKey, isValid) .

Why not just store "invalid username/password" status in a separate scope property?

$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.invalidUsernamePassword = true; // <--------------
        }
    });
    return false;
}

And the template will be simpler:

<div ng-show="invalidUsernamePassword">
    <span>Invalid username or password</span>
</div>

这是一个很好的服务器端验证指令,可能很有用https://groups.google.com/forum/#!msg/angular/6v-AillQboI/D0OaZzV7Me0J

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM