Repository navigation
Closed
Labels
area: formsfeatureLabel used to distinguish feature request from other issuesfeature: under considerationFeature request for which voting has completed and the request is now under considerationstate: Needs Design
Milestone
Description
I'm submitting a...
[ ] Regression (a behavior that used to work and stopped working in a new release)
[ ] Bug report
[x] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question
Current behavior
Right now form controls do not remember its original value.
Expected behavior
FormControl, FormGroup. FormArray:
- has property
readonly original: any
holding original value - has property
readonly differ: boolean
holdingtrueif value differ,falseotherwise - has property
readonly valueDiffer: Observable<boolean>
stream indicating if current value differs from original - has method
revert()/rollback()
changes value to original value - has method
commit()
set existing value as new original value
interface could looks like that:
interface EnhancedAbstractControl {
readonly original: any;
readonly differ: boolean;
readonly valueDiffer: Observable<boolean>;
commit(): void;
rollback(options?: { onlySelf?: boolean, emitEvent?: boolean });
}What is the motivation / use case for changing the behavior?
When creating form which updates current data it is also nice feature to allow user to revert changes for each individual field (instead of whole form) or set css class if value differ or show / hide other elements.
Sometimes also when user changes value from state A (original) -> B (end), some modal / warning need to be displayed now or when form is submitted or when value is changed. But for other values changes ex. A -> C or A -> D nothing happens. Validators are not candidates for this because value is valid.
With this new feature all this use cases will be easy to implement:
- rollback / revert
<input [formControl]="control">
<button *ngIf="control.valueDiffer | async" (click)="control.rollback()">Undo</button>- css
<div [class.differ]="control.differ">
<input [formControl]="control">
</div>- before submit
<form [formGroup]="form" (ngSubmit)="save()">
<input formControlName="control">
</form>save() {
const control = this.form.get('control');
if (control.original === "A" && control.value === "B") {
//ex. show modal
}
}Reacted by Emiddio Esposito, Fabian Wiles, Michał Stawicki, ToniaDemchuk, jlem, Ahn, Dominique Müller, Sandy, AlexBilonog, Daniel F. and 29 more
Reacted by SocVi100 and andy
Metadata
Metadata
Assignees
Labels
area: formsfeatureLabel used to distinguish feature request from other issuesfeature: under considerationFeature request for which voting has completed and the request is now under considerationstate: Needs Design
Activity
Toxicable commentedon Oct 16, 2017
These fields can be derived trivially from just adding in
originaleg:
this method is unneeded, just call
.setValue(control.original)I know these extra API's would make things slightly easier for the dev to use but from the core api aspect of it they're adding extra size to the bundle for no reason
Aside from the above I think this is a reasonable ask, there is currently no good ways to remember if the value has changed.
Current methods include:
mkurcius commentedon Oct 16, 2017
Indeed these extra API's would make things easier for the developers and I thought angular was created for this reason :)
I agree with that a
rollbackmethod can be easily replaced with existingsetValueorresetand dose not make much difference, but those 2 propertiesdifferandvalueDifferare more valuable.This solution:
requires 2 checks of
control.original != control.valueand 2 bindings,when this:
requires only 2 bindings (the value of differ can be calculated when setting new value in
updateValueAndValidityand only return here) and looks more elegantIf the form will be larger then more unnecessary checks will be executed and app will be slower. So instead of slightly larger bundle (I am only guessing) we will end up with slightly slower app.
Yes, to achieve this behavior in my project, I extended each of FormControl, FormGroup and FormArray but end up with classes which are calling private methods of is base class, which does not looks good :/
Toxicable commentedon Oct 16, 2017
I disagree, logical comparison is a very fast operation, unless your form has several million of these then I don't think you will see any slow downs.
Also by not having the proposed fields you're actually saving perf on people who don't use them since it wont need to do the comparison when you're not using it.
One other thing I missed
I also think this is unneeded, the original value should never change, if you need to update some value you should use the FormControl's value then send that wherever you need it to go.
originalis readonly so it cant be changed anywaybracco23 commentedon Oct 17, 2017
I have implemented a similar functionality, but instead of extending the FormControl class I have used a shadow deep copy of my model in the component where my form is.
From the developer usability point of view, a good strategy could be to create a new directive that adds this functionality to the wanted FormControl, allowing the developer to decide which fields need the rollback feature and which not. The same directive, if applied to a FormGroup or a FormArray, would apply the feature to all the contained FormControls.
mkurcius commentedon Oct 17, 2017
Yes, I agree with you that logical comparison is fast. My point here was that it is unnecessary check in template, and it forcing developers to write uglier code. If form has 1-2 controls it may not looks so bad, but if there are 5+ controls it becomes to by ugly (it is of course my personal opinion).
As you said logical comparison is fast, so even if one won't use it, he / she won't notice difference in performance.
The idea behind
commitis to handle situations like:User submit form, request is send to server with OK response, but after that user can still see the form and can make another changes. Now original values should be the ones which returned with the response or some others (depends on business case) and developer should be able to mark them as new original values. Maybe
originalis misleading name here, maybe instead oforiginalthis field should be calledsnapshotor something similar.The point here is to give user a experience that he / she change something, saved it and now he / she can change something again (maybe the same filed twice).
This experience can easily be achieved by adding class
differto some element and changing background-color to orange.The same situation as above but this time when submitting form server returns 409 (Conflict) or some other error. Error 409 may means optimistic locking exception and user will see modal with text "Inconsistent data" and buttons "Cancel" and "Refresh data". "Refresh data" button will re-fetch data and update form. From now on controls should have new
original/snapshotvalues, socommitmethod should be called right aftersetValuesto enable user experience the same as above.Without
commitit won't be possible, after form is updated (because of server response) controls will always be differ (have class .differ) and user won't be convinced everything is ok.25 remaining items
feat(forms): Allow a FormControl to use initial value as default. (#4…
dylhunn commentedon Feb 4, 2022
This was fixed in #44434. Eventually, reset-to-initial-value will become the default behavior.
angular-automatic-lock-bot commentedon Mar 7, 2022
This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.