ng-signal-forms icon indicating copy to clipboard operation
ng-signal-forms copied to clipboard

Setting values

Open timdeschryver opened this issue 2 years ago • 7 comments

We should support an API to update/set/patch the current value and/or controls of a form. This is already supported for form fields, but not for form groups.

timdeschryver avatar Nov 03 '23 11:11 timdeschryver

Do you have an idea for the API surface, of a set for starters? That is what I would think would be the first method to base things off of. Control values as WritableSignal<Value> naturally can be .set(...), but with form groups being Signal<UnwrappedFormGroup<Fields>>, then I imagine some method would be exposed on the group itself.

Provided I am correct in that assumption, A) What would you name the setter? B) Would the setter essentially just map the field values with whatever nesting level of the controls?

In my head it would be like

  form = createFormGroup<{ name: string; age: number | null }>({
    name: 'Alice',
    age: null,
  });

  someMethodThatSetsValue() {
    // Verbose name, but I imagine `.set()` would be confusing since it isn't a `WritableSignal`
    this.form.setFormGroupValue({ name: 'Jerry', age: 20 })
  }

michael-small avatar Mar 25 '24 22:03 michael-small

That seems good to me @michael-small ! For naming, I like the simplicity of set to be honest, but I'm also fine with setFormGroupValue.

timdeschryver avatar Mar 26 '24 18:03 timdeschryver

That seems good to me @michael-small !

I'll take a look into this approach then.

For naming, I like the simplicity of set to be honest, but I'm also fine with setFormGroupValue.

For the time being with my attempt I'll just start with the more verbose phrasing and if I actually get an implementation going then I suppose we would re-evaluate. Interested in hearing from anyone else reading this, since we are 1 vs 1 leaning on one solution but open to the other (minus the fact you are the owner and I am just some random person lol).

Edit: as an aside, it was nice to hear about this library referenced at different times at ng-conf this year. @timdeschryver did you hear some of the news/buzz on forms from the conference? Edit 2: or, were you there and I didn't see you?

michael-small avatar Mar 26 '24 19:03 michael-small

Oh cool, that's nice to hear. I wasn't at ng-conf and I wasn't aware that it was talked about in the hallway.

timdeschryver avatar Mar 27 '24 10:03 timdeschryver

Update on this issue: I have a somewhat working version of the group setter going. I think it is rather naive and missing some things, but it is coming along. I am comparing it with a reactive form on the same page with the form group filler and individual control prefills. I'm sure of course the assumptions are a bit different, I'll probably have questions on that later.

Aside about ng-conf: Yeah, ng-conf was nice. There was a lot of discussion about forms. It seems like all of the attempts to integrate forms with signals, a mix of template and reactive forms like this library, is what it sounds like to me that some people are speculating is the future. That's what I got from random chatter and gossip, and probably a lot of my bias in interpreting it. One of my favorite questions from the Q&A was about the future of forms. It sounds like the more immediate plan is to "bolt on" signals to the existing API, and then the future of the forms API is quite up for discussion and under wraps until then.

michael-small avatar Mar 27 '24 12:03 michael-small

Aw, no patchValue method yet :(

matheo avatar Dec 12 '24 21:12 matheo

@michael-small any updates on this :D?

rweintraub-cycleon avatar Feb 25 '25 14:02 rweintraub-cycleon