How do I reset a form including removing all validation errors?

前端 未结 10 827
梦毁少年i
梦毁少年i 2020-12-14 01:59

I have an Angular form. The fields are validated using the ng-pattern attribute. I also have a reset button. I\'m using the Ui.Utils Event Binder to handle the

相关标签:
10条回答
  • 2020-12-14 02:27

    I started with the comment from @Brett and built upon it. I actually have multiple forms and each form has many fields (more than just the two shown). So I wanted a general solution.

    I noticed that the Angular form object has a property for each control (input, select, textarea, etc) as well as some other Angular properties. Each of the Angular properties, though, begins with a dollar sign ($). So I ended up doing this (including the comment for the benefit of other programmers):

    $scope.reset = function(form) {
        // Each control (input, select, textarea, etc) gets added as a property of the form.
        // The form has other built-in properties as well. However it's easy to filter those out,
        // because the Angular team has chosen to prefix each one with a dollar sign.
        // So, we just avoid those properties that begin with a dollar sign.
        let controlNames = Object.keys(form).filter(key => key.indexOf('$') !== 0);
    
        // Set each control back to undefined. This is the only way to clear validation messages.
        // Calling `form.$setPristine()` won't do it (even though you wish it would).
        for (let name of controlNames) {
            let control = form[name];
            control.$setViewValue(undefined);
        }
    
        form.$setPristine();
        form.$setUntouched();
    };
    
    0 讨论(0)
  • 2020-12-14 02:27

    further to @battmanz 's answer, but without using any ES6 syntax to support older browsers.

     $scope.resetForm = function (form) {
    
                try {
                    var controlNames = Object.keys(form).filter(function (key) { return key.indexOf('$') !== 0 });
    
                    console.log(controlNames);
                    for (var x = 0; x < controlNames.length; x++) {
                        form[controlNames[x]].$setViewValue(undefined);
                    }
    
                    form.$setPristine();
                    form.$setUntouched();
                } catch (e) {                
                    console.log('Error in Reset');
                    console.log(e);
                }
    
            };
    
    0 讨论(0)
  • 2020-12-14 02:33

    So none of the answers were completely working for me. Esp, clearing the view value, so I combined all the answers clearing view value, clearing errors and clearing the selection with j query(provided the fields are input and name same as model name)

     var modelNames = Object.keys($scope.form).filter(key => key.indexOf('$') !== 0);
                        modelNames.forEach(function(name){
                            var model = $scope.form[name];
                            model.$setViewValue(undefined);
                            jq('input[name='+name+']').val('');
                            angular.forEach(model.$error, function(value, name) {
                              // reset validity
                              model.$setValidity(name, null);
                            });
                        });
                        $scope.form.$setPristine();
                        $scope.form.$setUntouched();
    
    0 讨论(0)
  • 2020-12-14 02:38

    You can pass your loginForm object into the function ng-click="userCtrl.login(loginForm) and in the function call

    this.login = function (loginForm){
      loginForm.$setPristine();
      loginForm.$setUntouched();
    }
    
    0 讨论(0)
提交回复
热议问题