last day (15 days later) » 

8:28 AM
0
Q: Angular - Reactive form, how to correctly store the initial value, and check for changes in the form?

AJ989I'm using reactive forms with angular 6 and I have 2 questions. First one, what's the correct way to store the initial value of a form? I'm pretty new to angular, and what I've done so far, is to declare in the controller all the form code, and then in the view I bind those controls with the fr...

 
Try first then come with specific questions if you struggle ! You can find a very nice tutorial, I even googled one for you : intertech.com/Blog/angular-reactive-forms-tutorial
 
thank you, my specific question is mostly on how to check if a field of my form has not changed (pristine/dirty is not enough), so if a user changes something, but then changes it back before saving the form, the Save button should be disabled because the form is the same as before!
 
If you create a stackblitz project, I'll be glad to show you a way to implement what you want
 
is there around a premade stackblitz with just formGroups? as my form is huge with lots of validators and it would be painful to put it on stackblitz! One field would be more than enough to just understand the logic on how to check if the value has really changed! Gonna quickly look for a premade stackblitz with formGroup
here a stackblizt with a single formgroup and a formArray, and a save button that for now is disabled when the form is pristine stackblitz.com/edit/…
 
Do you work with material component ? If no, I will check for another stackblitz or create one
 
8:28 AM
yes, I work with material and I made that stackblitz with parts of my big reactive form!
 
Ok I'm going to check then !
 
thank you very much
 
9:06 AM
I added the modified stackblitz
 
great, saw it, and it s clear, thank you very much! Wouldn't be good for my purpose to always work with a clone? So the user opens the form, which is prefilled based on other user's inputs, then fills it out, but only if he actually saves the form,
the form is going to be copied onto my reactive form control, otherwise it will be discarded and the form will return to its original status?
 
9:22 AM
I noticed that you were using type="text", so I modified the stackblitz. I'm not sure to understand why you want to have two forms ? You have your data, you have your form, why do you want an other form ?
 
So let me try to explain, I'm not sure I want it, but I think I might need it! Isn't it bad practice to manipulate directly data on the actual form? So when the user is typing something in a input box, that value goes directly to the form correct? Wouldn't it be a good practice to instead make a copy of the form when it's initialized, then the users fills the input boxes, and only when he click "Save", the copy form gets written over the original form?
 
You do not "manipulate your data", your data variable does not change (I use it to restore your form initial values and do a deep dirty-check in the stackblitz). The purpose of a form is to be modified by the user, and when the user is satisfied with its changes, you just save the form (if the form is valid).
A form purpose is just a section where you give user freedom to send data to the server. You do not need to clone it at all ! :)
 
perfect all clear! thank you very much for the help and explanation! Going to mark your answer as correct! thank you!
 

last day (15 days later) »