question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using form parent properties in transformFrom/To

See original GitHub issue

Hi there,

i just migrated my application from the old to the new api. A few forms behaved differently after the migration but i found the issue quickly. I know that this is not really a bug but i still want to share it and maybe spark a discussion.

angular@13 ngx-sub-form@5.3.2

With the old inheritance-based api i was able to pass additional data via @Input() into my component and since my component extended NgxAutomaticRoot/NgxRoot/NgxSubFormComponent i could use this data inside the transformToFormGroup and transformFromFormGroup functions.

@Input('configurations')
public configurations: Configuration[] = [];
...

protected transformToFormGroup(client: Client | null,  defaultValues: Partial<ClientForm> | null): ClientForm | null {
  if (!client) {
    return null;
  }
  const configuration =  this.configurations.find((configuration) => configuration.id === client.configurationId) ?? null;
  return {
    name: client.name,
    configuration: configuration,
  };
}

So after migrating to the new api all the forms which had such additional @Input properties failed because this simply is in the wrong context when transformToFormGroup is called, thus this.configurations is undefined. In order to fix this i had to wrap my transformToFormGroup inside a construction function which accepts and keeps backreference to my component. Think of it as a common var that = this; situation.

interface TransformFunc {
  (client: Client | null): ClientForm | null;
}
...
private backReferencedTransformToFormGroup = ( backreference: any ): TransformFunc => {
   return (client: Client | null): ClientForm | null => {
      if (!client) {
         return null;
      }
      const configuration = backreference.configurations.find((configuration) => configuration.id === client.configurationId ) ?? null;
      return {
        name: '',
        configuration: configuration,
      };
   };
};
...
public form = createForm<Client, ClientForm>(this, {
    ...
    toFormGroup: this.backReferencedTransformToFormGroup(this),
});

Check a full example at https://stackblitz.com/edit/angular-ivy-t8fvta

Unsurprisingly this is also the case for transformFromFormGroup and i can even think of a lot more cases where a backreference is needed since it is not an @Input()-dependent problem but applies to all cases where a transformFunction needs access to the actual component properties.

Depending on the use-case/complexity one could also do this kind of mapping/transformation in the parent-component and not the form-component. But i suppose that’s why ngx-subform allows us to define a function for toFormGroup and fromFormGroup in the first place.

Issue Analytics

  • State:closed
  • Created 9 months ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
lgaidacommented, Dec 7, 2022

Yes, closing this issue is totally fine. Thanks for reaching out again!

0reactions
maxime1992commented, Dec 7, 2022

Oh yes, well spotted! I think it’s now ok to close this issue as you’ve got your answer?

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I pass the FormGroup of a parent component to its ...
... the parent from the child component, you can access parent property of the FormControl instance, https://angular.io/api/forms/AbstractControl#parent.
Read more >
Parent property (Microsoft Forms)
Use the Parent property to access the properties, methods, or controls of an object's parent. This property is useful in an application in...
Read more >
Sharing data between child and parent directives ... - Angular
The @Input() decorator in a child component or directive signifies that the property can receive its value from its parent component.
Read more >
INSTRUCTIONS FOR COMPLETING THE PARENT/CHILD ...
THE PARENT/CHILD EXCLUSION CLAIM FORM. A. PROPERTY. Assessor's Parcel Number: Enter all the parcel numbers that are involved in this specific transfer. On...
Read more >
Form Properties - PowerSchool Documentation Server
This property is used to send pre-registration form notifications to parents and guardians. The property requires a valid email element on the form....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found