AngularJS - Server side validation and client side forms

前端 未结 8 739
忘了有多久
忘了有多久 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条回答
  • 2020-12-12 12:09

    As variant

    // ES6 form controller class
    
    class FormCtrl {
     constructor($scope, SomeApiService) {
       this.$scope = $scope;
       this.someApiService = SomeApiService;
       this.formData = {};
     }
    
     submit(form) {
       if (form.$valid) {
         this.someApiService
             .save(this.formData)
             .then(() => {
               // handle success
    
               // reset form
               form.$setPristine();
               form.$setUntouched();
    
               // clear data
               this.formData = {};
             })
             .catch((result) => {
               // handle error
               if (result.status === 400) {
                 this.handleServerValidationErrors(form, result.data && result.data.errors)
               } else {// TODO: handle other errors}
             })
       }
     }
    
     handleServerValidationErrors(form, errors) {
      // form field to model map
      // add fields with input name different from name in model
      // example: <input type="text" name="bCategory" ng-model="user.categoryId"/>
      var map = {
        categoryId: 'bCategory',
        // other
      };
    
      if (errors && errors.length) {
        // handle form fields errors separately
        angular.forEach(errors, (error) => {
          let formFieldName = map[error.field] || error.field;
          let formField = form[formFieldName];
          let formFieldWatcher;
    
          if (formField) {
            // tell the form that field is invalid
            formField.$setValidity('server', false);
    
            // waits for any changes on the input
            // and when they happen it invalidates the server error.
            formFieldWatcher = this.$scope.$watch(() => formField.$viewValue, (newValue, oldValue) => {
              if (newValue === oldValue) {
                return;
              }
    
              // clean up the server error
              formField.$setValidity('server', true);
    
              // clean up form field watcher
              if (formFieldWatcher) {
                formFieldWatcher();
                formFieldWatcher = null;
              }
            });
          }
        });
    
      } else {
        // TODO: handle form validation
        alert('Invalid form data');
      }
    }
    
    0 讨论(0)
  • 2020-12-12 12:17

    As of July 2014, AngularJS 1.3 has added new form validation features. This includes ngMessages and asyncValidators so you can now fire server side validation per field prior to submitting the form.

    Angular 1.3 Form validation tutorial :

    • Taming forms in Angular 1.3
    • Video | Repo | Demo

    References:

    • ngMessages directive
    • ngModel.NgModelController
    0 讨论(0)
提交回复
热议问题