Skip to content
This repository has been archived by the owner on Jul 21, 2023. It is now read-only.

Control type doesn't work with setValue #76

Closed
ArielGueta opened this issue Jun 30, 2020 · 9 comments
Closed

Control type doesn't work with setValue #76

ArielGueta opened this issue Jun 30, 2020 · 9 comments
Labels
project: forms Relates to the project forms type: bug/fix Something isn't working

Comments

@ArielGueta
Copy link

I have the following form:

    const form = new FormGroup<{
      skills: Control<string[]>
    }>({
      skills: new FormControl(['a', 'b', 'c'])
    });

    form.setValue({ skills: ['1', '2', '3'] })

And I get an error from setValue.

Type 'string[]' is not assignable to type 'Control<string[]>'.   Property '[sym]' is missing in type 'string[]' but required in type 'UniqToken'.

@KostyaTretyak
Copy link
Owner

Use Control<T> for object type only. Don't use it for an array.

@ArielGueta
Copy link
Author

So what should I use?

@KostyaTretyak
Copy link
Owner

KostyaTretyak commented Jun 30, 2020

const form = new FormGroup<{ skills: string[] }>({
  skills: new FormArray([new FormControl('a'), new FormControl('b'), new FormControl('c')]),
});

form.setValue({ skills: ['1', '2', '3'] });

@KostyaTretyak KostyaTretyak added project: forms Relates to the project forms type: discussion / question Further information is requested labels Jun 30, 2020
@ArielGueta
Copy link
Author

Why you're closing the issue? This is not the intended behavior. I have a FormControl with an array value.

@ArielGueta
Copy link
Author

A custom control accessor can be FormControl that takes an array.

@KostyaTretyak KostyaTretyak reopened this Jun 30, 2020
@KostyaTretyak KostyaTretyak added type: bug/fix Something isn't working and removed type: discussion / question Further information is requested labels Jun 30, 2020
@KostyaTretyak
Copy link
Owner

Sorry. In my practice, it has not been the case that FormControl requires an array, but it may really be necessary. Now I will try to fix it. This is a bug.

@KostyaTretyak
Copy link
Owner

You can do this:

const form = new FormGroup<{ skills: Control<string[]> }>({
  skills: new FormControl(['a', 'b', 'c']),
});

form.get('skills').setValue(['1', '2', '3']);

@ArielGueta
Copy link
Author

Yes, but In reality, I have more properties in the group so it's more convenient to use setValue on the group. I'll wait for a fix. Thanks.

@ismailkoksal
Copy link

ismailkoksal commented Jul 2, 2020

Having the same problem

interface ProfileForm {
  name: string;
  availableWays: Control<Way[]>;
  displayedWays: Control<Way[]>;
}

this.form = this.fb.group<ProfileForm>({
      name: [{value: '', disabled: true}],
      availableWays: [{value: [], disabled: true}],
      displayedWays: [{value: [], disabled: true}]
    });

this.form.reset({
      name: '',
      availableWays: [...this.ways], // ERROR : Type 'Way[]' is not assignable to type 'Control<Way[]>'.   Property '[sym]' is missing in type 'Way[]' but required in type 'UniqToken'.
      displayedWays: [], // ERROR : Type 'Way[]' is not assignable to type 'Control<Way[]>'.   Property '[sym]' is missing in type 'Way[]' but required in type 'UniqToken'.
    });

This is working :

this.form.reset({
      name: '',
      availableWays: [...this.ways],
      displayedWays: [],
    } as ProfileForm);

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
project: forms Relates to the project forms type: bug/fix Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants