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

前端 未结 10 931
梦毁少年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:15

    I had the same problem and tried to do battmanz solution (accepted answer).

    I'm pretty sure his answer is really good, but however for me it wasn't working.

    I am using ng-model to bind data, and angular material library for the inputs and ng-message directives for error message , so maybe what I will say will be useful only for people using the same configuration.

    I took a lot of look at the formController object in javascript, in fact there is a lot of $ angular function as battmanz noted, and there is in addition, your fields names, which are object with some functions in its fields.

    So what is clearing your form ?

    Usually I see a form as a json object, and all the fields are binded to a key of this json object.

    //lets call here this json vm.form
    vm.form = {};
    
    //you should have something as ng-model = "vm.form.name" in your view
    

    So at first to clear the form I just did callback of submiting form :

    vm.form = {};
    

    And as explained in this question, ng-messages won't disappear with that, that's really bad.

    When I used battmanz solution as he wrote it, the messages didn't appear anymore, but the fields were not empty anymore after submiting, even if I wrote

    vm.form = {};
    

    And I found out it was normal, because using his solution actually remove the model binding from the form, because it sets all the fields to undefined. So the text was still in the view because somehow there wan't any binding anymore and it decided to stay in the HTML.

    So what did I do ?

    Actually I just clear the field (setting the binding to {}), and used just

    form.$setPristine();
    form.$setUntouched();
    

    Actually it seems logical, since the binding is still here, the values in the form are now empty, and angular ng-messages directive is triggering only if the form is not untouched, so I think it's normal after all.

    Final (very simple) code is that :

    function reset(form) {
            form.$setPristine();
            form.$setUntouched();
    };
    

    A big problem I encountered with that :

    Only once, the callback seems to have fucked up somewhere, and somehow the fields weren't empty (it was like I didn't click on the submit button).

    When I clicked again, the date sent was empty. That even more weird because my submit button is supposed to be disabled when a required field is not filled with the good pattern, and empty is certainly not a good one.

    I don't know if my way of doing is the best or even correct, if you have any critic/suggestion or any though about the problem I encountered, please let me know, I always love to step up in angularJS.

    Hope this will help someone and sorry for the bad english.

提交回复
热议问题