Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NgxFormWithArrayControls access formControlName ? #197

Closed
elvispdosreis opened this issue Nov 18, 2020 · 7 comments
Closed

NgxFormWithArrayControls access formControlName ? #197

elvispdosreis opened this issue Nov 18, 2020 · 7 comments
Assignees
Labels
flag: can be closed? This issue or PR should probably be closed now type: RFC/discussion/question This issue is about RFC, discussion or a question

Comments

@elvispdosreis
Copy link

I have a component created with NgxFormWithArrayControls and I need in ngfor to access the form control name

the examples that I have seen and transform the input into another form component to do this, but it is so simple that there is no need to transform it into a component

<ul>
	<li *ngFor="let user of formGroupControls.users; let i = index;">
		<input formControlName="user.name">
	</li>
</ul>
@maxime1992
Copy link
Collaborator

Hi @elvispdosreis

is there a question here?

@maxime1992 maxime1992 added the flag: can be closed? This issue or PR should probably be closed now label Nov 18, 2020
@elvispdosreis
Copy link
Author

Hi @elvispdosreis

is there a question here?

Yes

@elvispdosreis elvispdosreis changed the title NgxFormWithArrayControls access formControlName NgxFormWithArrayControls access formControlName ? Nov 18, 2020
@maxime1992
Copy link
Collaborator

The reason you have to create a new component is because ngx-sub-form is not aware of your nested data.

The good thing about nested forms (and behind the scenes ControlValueAccessor) is that it lets us deal with only the first layer of an object and we can either use that as it is (an object) or delegate the breakdown of this object to another sub form.

Otherwise, your idea of sub forms would be broken and we'd have to deal with loads of data in 1 component.

So from there, you have 2 options:

  • Either use another sub form which you don't seem to want as you'd have to create a sub form for apparently only 1 property
  • Use the remap function of ngx-sub-form to put the user name at the first level of your current form

Please read the doc for the second solution first. If that doesn't make sense I'm happy to help if you provide first a minimal reproduction on stackblitz

@maxime1992 maxime1992 added the state: needs repro This issue needs a repro label Nov 19, 2020
@elvispdosreis
Copy link
Author

I need to edit the component in the table to access it with <input [formControlName]="compartment.size">
sample

@maxime1992
Copy link
Collaborator

Can you explain a bit more in details what I am supposed to look at in your stackblitz please? For example, explain what the current state is, what is not working and/or what you'd like to do

@elvispdosreis
Copy link
Author

I would like to link this grid to the form

image

@maxime1992 maxime1992 added type: RFC/discussion/question This issue is about RFC, discussion or a question and removed state: needs repro This issue needs a repro labels Dec 29, 2020
@maxime1992 maxime1992 self-assigned this Dec 29, 2020
@maxime1992
Copy link
Collaborator

@elvispdosreis you cannot do

	<tbody>
		<tr *ngFor="let compartment of formGroupControls.compartments.controls; let i = index;">
			<td>{{i}}</td>
			<td>{{compartment.value.deck}}</td>
			<td><input [formControlName]="compartment.size"></td> <--------------- this

Compartment is a FormControl. Remember how you created it?

By calling addCompartment which itself calls createFormArrayControl which itself does:

      case "compartments":
        return new FormControl(value, [Validators.required]);

So when you do *ngFor="let compartment of formGroupControls.compartments.controls; let i = index;" the compartment variable is a FormControl. And a FormControl doesn't have a size property (or at least not the one you'd expect). BUT, it holds a value (which here is an object containing a deck and a size property).

To allow the modification of the value property you need to create a sub form and pass this whole FormControl then allow only to edit the size property from there.

I've made you an example here:

https://stackblitz.com/edit/ngx-sub-form-array-remap-yuz193?file=src%2Fapp%2Fspaceship-container%2Fspaceship-form%2Fspaceship-config-form%2Fcompartments-form%2Fspaceship-compartments-form.component.html

Note: While the value is binded correctly initially, note that if you update the value from the table, it won't update the value in the form above :/ But I believe that you're not hitting a limitation from ngx-sub-form but rather angular itself: angular/angular#13792

I've also commented on this issue with a minimal repro: angular/angular#13792 (comment)

I believe this is the same limitation in this case.

I'm going to close this as everything has been answered, feel free to reopen if it doesn't :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flag: can be closed? This issue or PR should probably be closed now type: RFC/discussion/question This issue is about RFC, discussion or a question
Projects
None yet
Development

No branches or pull requests

2 participants