formly-js / angular-formly

JavaScript powered forms for AngularJS
http://docs.angular-formly.com
MIT License
2.22k stars 405 forks source link

resetModel() does not reset the $pristine and $dirty flag on the main form #574

Closed abhisheksaraiya closed 8 years ago

abhisheksaraiya commented 8 years ago

Calling resetModel() on the options resets the field $pristine and $untouched values on each field nicely but the $pristine on the main form is left unchanged. Is there a reason why these are left out in the resetModel call?

kentcdodds commented 8 years ago

resetModel should update those values. Could you please reproduce your issue using issue.angular-formly.com? It will help us determine what exactly the problem is that you're describing and how it could be fixed. Thanks!

abhisheksaraiya commented 8 years ago

Here are the steps to reproduce the issue: http://jsbin.com/jonocawunu/edit?html,js,output

1) In the beginging when the page is loaded in jsbin, please note the values $pristine and $dirty for FORM with name : "vm.form". They would be Values for FORM: "$dirty": false, "$pristine": true, Values for input field: "$dirty": false, "$pristine": true,

2) Now if you write anything in the input box in the form the values will change to Values for FORM: "$dirty": true, "$pristine": false, Values for input field: "$dirty": true, "$pristine": false,

This is OK!

3) Now when you click on reset button (thus calling resetModel()), the values change to: Values for FORM: "$dirty": true, "$pristine": false, Values for input field: "$dirty": false, "$pristine": true,

The issue is that after resetModel is called, the values for the input field are reset as expected but for the form ($pristine,$dirty) are not reset!

kentcdodds commented 8 years ago

Looks like after we call resetModel on all the individual fields (right here) we need to set the form's touched and pristine state like we do with the individual fields (here).

I'm open to a pull request for this! Anyone's welcome to try. Just follow these instructions

tgrecojs commented 8 years ago

@kentcdodds is this issue still open? I checked it out the other day but i'm just seeing that I misread the comments :hear_no_evil:

kentcdodds commented 8 years ago

This issue should be closed I think. Let me know if you experience otherwise!

janneman001 commented 8 years ago

I still see this in formly 7.2.3. after resetModel vm.form.$pristine equals false and vm.form.$dirty equals true. They should represent a propagated calculation from the fields $pristine and $dirty.

kentcdodds commented 8 years ago

@janneman001 could you provide an example of this behavior?

janneman001 commented 8 years ago

I retested with latest 7.3.9. Same behaviour. I have no experience with js bin. But i will try. I experienced the issue in a form with mysql backend code. I will try to make a smaller reproduction example if time permits.

kentcdodds commented 8 years ago

Here you can use this as a starter, then just post the link in here :+1:

janneman001 commented 8 years ago

My bad. I was fighting with an incorrect datepicker type field. I re-verified with 7.3.9 it is alright in 7.3.9.