Skip to content
This repository was archived by the owner on Sep 5, 2024. It is now read-only.
This repository was archived by the owner on Sep 5, 2024. It is now read-only.

Form does not revert to initial state when submitted #1903

Closed

Description

@axelson

When I submit a form that I want to re-use and reset the inputs I expect 'ng-invalid-required' to no longer be set on the form. Instead it is still set and the form is shown as "red".

Here's a small screencast showing the problem:

small screencast

Here's a code pen:
http://codepen.io/anon/pen/ogaROy

I'm resetting the form by resetting the model:

  $scope.reset = function() {
    $scope.user = {
      address: ''
    }
  }

However I've also tried $setPristine() and that didn't help either. Is there something I'm missing or is this a bug?

This issue is potentially related: #931

Activity

  1. andreabadesso commented on Mar 14, 2015

    @andreabadesso

    You can workaround it by giving a name to the form and input and setting:

    $touched = false;

        <form style="padding: 20px;" ng-submit="reset()" name="addressForm">
          <md-input-container><label>Address</label>
            <input type="text"
                   ng-model="user.address"
                   name="address"
                   required />
          </md-input-container>
    
      $scope.reset = function() {
        $scope.user = {};
        $scope.addressForm.address.$touched = false;
      };
    
  2. mxab commented on Mar 14, 2015

    @mxab

    Hey, I have the same problem,
    but @andreabadesso the named property in my form does not seem to be there, so the $touched property is also not there

  3. erikdonohoo commented on Mar 15, 2015

    @erikdonohoo

    @andreabadesso the named property is based on the name property you give it. Give your form and each input in it a name, and then you can do the same thing.

  4. axelson commented on Mar 17, 2015

    @axelson
    Author

    I updated the code pen giving a name to the form and to the input and indeed setting $touched = false does work, except that it is not resetting the md-char-counter, is that a bug.

    http://codepen.io/anon/pen/ogaROy

    Also for some reason setting $touched = false does not work in my full environment, even though it does work in the code-pen, I'm not sure what might cause that.

  5. gkalpak commented on Mar 19, 2015

    @gkalpak
    Member

    Adding a little context:

    By default, mdInputContainer displays its errors only if the containing input has been $touched (focused and blurred). (BTW, you can change this default behaviour, with the mdIsError attribute.)
    After you submit your form, and reset/empty your model, the fields are still $touched, thus errors will be displayed.

    If you want to reset the whole form (i.e. all mdInputContainers in it), you can call $setUntouched() on the formController, as shown here.

    mdMaxlength's not resetting the counter to 0, is indeed a bug with mdMaxlength (which needs some love anyway).

  6. added this to the 0.10.0 milestone on Apr 17, 2015
  7. modified the milestones: 0.10.0, 0.11.0 on Jun 16, 2015
  8. modified the milestones: 0.11.0, on Jul 31, 2015
  9. removed this from the milestone on Aug 10, 2015
  10. 12 remaining items

  11. modified the milestones: , on Jan 5, 2016
  12. wembernard commented on Apr 8, 2016

    @wembernard

    Some reset function would definitely be appreciable. Don't have time to figure out why but suggested workarounds here by @andreabadesso & @gkalpak didn't work.

    I tried in the console to play with either the form or the input and $touched = false, $valid = true, $invalid = false (wondering btw why those 2 exist), $setPristline(), $setUntouched()... but didn't get any positive results.

    I don't have time to dig more into this but that's annoying.

    angular-material#1.0.7
    
  13. garaboncias commented on Apr 19, 2016

    @garaboncias

    form.$setUntouched() is works for input field's border but not for ng-messages:
    http://codepen.io/anon/pen/MyXmpN

    1. focus in the address field
    2. submit (it will reset the form)
      expected: ng-messages also become hidden
      actual: visible however the form field shows as a untouched (therefore valid) field
  14. modified the milestones: , Deprecated on Apr 20, 2016
  15. jazdw commented on Oct 6, 2016

    @jazdw

    Can we re-open this, its still broken in 1.1.1

  16. Jay0328 commented on Oct 15, 2016

    @Jay0328

    I also still have this problem.
    Hope to repair this.

  17. dnsmob commented on Oct 16, 2016

    @dnsmob

    you most likely didn't import 'angular-messages' module. i was also having the same issue, but just adding that fixed everything. have a look at this https://scotch.io/tutorials/angularjs-form-validation-with-ngmessages. i know it's pure angular, but it works the same, no need to check $touched or any other sorcery.

  18. ThomasBurleson commented on Oct 17, 2016

    @ThomasBurleson
    Contributor

    This issue is closed as it falls in the 'deprecated' category. We deprecate issues for one or more of the following reasons:

    • Issue has a reasonable workaround
    • Issue doesn't have a demo or reproducible case available
    • Issue is real, but fixing it is risky for users who depend on existing behavior
    • Issue is real, but only affects a very small use case
    • Issue is real, but would require significant work to fix, and is less urgent than other needs.

    Pull Requests are welcomed to resolve this issue. But note that not all community PRs will be merged into master. Pull Requests with large changes or large scope impacts will probably be rejected [due to stability risks to existing users].

  19. locked and limited conversation to collaborators on Oct 17, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions