Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

form and input is getting dirty by clicking on a simple button #7457

Closed
westor21 opened this issue May 14, 2014 · 9 comments
Closed

form and input is getting dirty by clicking on a simple button #7457

westor21 opened this issue May 14, 2014 · 9 comments

Comments

@westor21
Copy link

Create a simple form with an input and a button without any functionality. Clicking the button results in setting the form and the input to dirty.
To illustrate the behaviour I made a plunkr:
http://plnkr.co/edit/GlLD83K8OLVIShPlfENm?p=preview
Best regards
westor21

@caitp
Copy link
Contributor

caitp commented May 14, 2014

http://www.whatwg.org/specs/web-apps/current-work/multipage/the-button-element.html#attr-button-type

The type attribute controls the behavior of the button when it is activated. It is an enumerated attribute. The following table lists the keywords and states for the attribute — the keywords in the left column map to the states in the cell in the second column on the same row as the keyword.

Keyword State Brief description
submit Submit Button Submits the form.
reset Reset Button Resets the form.
button Button Does nothing.
menu Menu Shows a menu.

The missing value default is the Submit Button state.

If the type attribute is in the Submit Button state, the element is specifically a submit button.

Basically, when you don't give your button a type, it defaults to "submit", and angular's submission handler causes the form to be dirty'd.

You have an easy work around, just give the button a type, like type="button", that should prevent form submission and avoid dirtying the form/model controllers.

@petebacondarwin / @matsko should we be dirtying the form on submit if it doesn't change? that seems weird.

@caitp caitp added this to the 1.3.0 milestone May 15, 2014
@westor21
Copy link
Author

Thank you for your answer. And yes, that seems to be not the expected behaviour to me. We are setting classes in dependence a form element is touched by the user. Therefore it seems to be weird to set the form and the elements dirty if nothing was edited.

@caitp caitp self-assigned this May 15, 2014
@shahata
Copy link
Contributor

shahata commented May 16, 2014

@caitp @petebacondarwin the form is becoming dirty due my bug in a0ae07b ($commitViewValue always sets the control as dirty and now it is called with every submit). I'll take a look into this.

@petebacondarwin
Copy link
Contributor

Thanks @shahata

RichardLitt pushed a commit to RichardLitt/angular.js that referenced this issue May 24, 2014
…as changed

Calling `$commitViewValue` was was dirtying the input, even if no update to the view
value was made.
For example, `updateOn` triggers and form submit may call `$commitViewValue` even
if the the view value had not changed.

Closes angular#7457
Closes angular#7495
@NaomiN
Copy link

NaomiN commented Sep 20, 2017

I am using angular-js 1.6.4 and I am getting the same problem. May be the issue re-appeared. I fixed it by setting my buttons type to be button instead of submit, but the issue marked as closed so I should not need to have done it, right?

@petebacondarwin
Copy link
Contributor

@NaomiN the original issue is not broken with 1.6.4. See http://plnkr.co/edit/EAAdJScYtRJKich5FJQs?p=preview

If you are experiencing a different issue then please can you create a new issue with a reproduction.

@NaomiN
Copy link

NaomiN commented Sep 20, 2017

In the original plunker I made a single change - the input type="number" to demonstrate the problem. The problem only shows for input type="number" and buttons. I made a google search and found this issue. So my solution was to use button type="button"

@petebacondarwin
Copy link
Contributor

I just tried that on my new plunker and it still doesn't mark it as dirty.

@NaomiN
Copy link

NaomiN commented Sep 20, 2017

Weird, I can no longer reproduce myself there even though it marked as dirty this morning. I know I spent several hours last night trying to figure out the problem with my form before landing on this issue.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants