AngularJS - Server side validation and client side forms

前端 未结 8 746
忘了有多久
忘了有多久 2020-12-12 11:40

I am trying to understand how to do the following things:

What is the accepted way of declaring a form. My understanding is you just declare the form in HTML, and ad

8条回答
  •  Happy的楠姐
    2020-12-12 11:51

    I've also been playing around with this kind of thing recently and I've knocked up this demo. I think it does what you need.

    Setup your form as per normal with any particular client side validations you want to use:

    You must enter a value here {{myForm.firstName.$error.serverMessage}}
    {{myForm.lastName.$error.serverMessage}}

    Note also I have added a serverMessage for each field:

    {{myForm.firstName.$error.serverMessage}}
    

    This is a customisable message that comes back from the server and it works the same way as any other error message (as far as I can tell).

    Here is the controller:

    function MyCtrl($scope, $parse) {
          var pretendThisIsOnTheServerAndCalledViaAjax = function(){
              var fieldState = {firstName: 'VALID', lastName: 'VALID'};
              var allowedNames = ['Bob', 'Jill', 'Murray', 'Sally'];
    
              if (allowedNames.indexOf($scope.firstName) == -1) fieldState.firstName = 'Allowed values are: ' + allowedNames.join(',');
              if ($scope.lastName == $scope.firstName) fieldState.lastName = 'Your last name must be different from your first name';
    
              return fieldState;
          };
          $scope.submit = function(){
              var serverResponse = pretendThisIsOnTheServerAndCalledViaAjax();
    
              for (var fieldName in serverResponse) {
                  var message = serverResponse[fieldName];
                  var serverMessage = $parse('myForm.'+fieldName+'.$error.serverMessage');
    
                  if (message == 'VALID') {
                      $scope.myForm.$setValidity(fieldName, true, $scope.myForm);
                      serverMessage.assign($scope, undefined);
                  }
                  else {
                      $scope.myForm.$setValidity(fieldName, false, $scope.myForm);
                      serverMessage.assign($scope, serverResponse[fieldName]);
                  }
              }
          };
    }
    

    I am pretending to call the server in pretendThisIsOnTheServerAndCalledViaAjax you can replace it with an ajax call, the point is it just returns the validation state for each field. In this simple case I am using the value VALID to indicate that the field is valid, any other value is treated as an error message. You may want something more sophisticated!

    Once you have the validation state from the server you just need to update the state in your form.

    You can access the form from scope, in this case the form is called myForm so $scope.myForm gets you the form. (Source for the form controller is here if you want to read up on how it works).

    You then want to tell the form whether the field is valid/invalid:

    $scope.myForm.$setValidity(fieldName, true, $scope.myForm);
    

    or

    $scope.myForm.$setValidity(fieldName, false, $scope.myForm);
    

    We also need to set the error message. First of all get the accessor for the field using $parse. Then assign the value from the server.

    var serverMessage = $parse('myForm.'+fieldName+'.$error.serverMessage');
    serverMessage.assign($scope, serverResponse[fieldName]);
    

    Hope that helps

提交回复
热议问题