Skip to content

[Proposal] Initial / Original value API for AbstractControl #19747

Closed

Description

@mkurcius

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
    holding true if value differ, false otherwise
  • 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
  }
}

Activity

  1. Toxicable commented on Oct 16, 2017

    @Toxicable

    readonly differ: boolean;
    readonly valueDiffer: Observable;

    These fields can be derived trivially from just adding in original
    eg:

    [class.differ]="control.original != control.value"
    *ngIf="control.original != control.value"
    

    rollback(options?: { onlySelf?: boolean, emitEvent?: boolean });

    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:

    • holding a mirrored object of the FormGroup/Control with just the original values to do a lookup
    • Extending FormControl
  2. mkurcius commented on Oct 16, 2017

    @mkurcius
    Author

    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

    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 rollback method can be easily replaced with existing setValue or reset and dose not make much difference, but those 2 properties differ and valueDiffer are more valuable.

    This solution:

    [class.differ]="control.original != control.value"
    *ngIf="control.original != control.value"
    

    requires 2 checks of control.original != control.value and 2 bindings,

    when this:

    [class.differ]="control.differ"
    *ngIf="control.differ"
    

    requires only 2 bindings (the value of differ can be calculated when setting new value in updateValueAndValidity and only return here) and looks more elegant

    If 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.

    Current methods include:

    • holding a mirrored object of the FormGroup/Control with just the original values to do a lookup
    • Extending FormControl

    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 :/

  3. Toxicable commented on Oct 16, 2017

    @Toxicable

    If 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.

    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

    commit(): void;

    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.
    original is readonly so it cant be changed anyway

  4. bracco23 commented on Oct 17, 2017

    @bracco23

    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.

  5. mkurcius commented on Oct 17, 2017

    @mkurcius
    Author

    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.

    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.

    One other thing I missed

    commit(): void;

    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.
    original is readonly so it cant be changed anyway

    The idea behind commit is to handle situations like:

    1. 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 original is misleading name here, maybe instead of original this field should be called snapshot or 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 differ to some element and changing background-color to orange.

    2. 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 / snapshot values, so commit method should be called right after setValues to enable user experience the same as above.

    Without commit it 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.

  6. 25 remaining items

  7. dylhunn commented on Feb 4, 2022

    @dylhunn
    Contributor

    This was fixed in #44434. Eventually, reset-to-initial-value will become the default behavior.

  8. angular-automatic-lock-bot commented on Mar 7, 2022

    @angular-automatic-lock-bot

    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.

  9. locked and limited conversation to collaborators on Mar 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: formsfeaturefeature: under considerationstate: Needs Design

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions