1
votes

I have a User form of reactive forms and I have used RxFormBuilder from @rxweb package , I have a user model class which contains UserInfo object, how can I use it as a nested formgroup?

model.ts:

import {prop,propArray } from "@rxweb/reactive-form-validators"
export class UserInfo{
   @prop()
   cityId: number 

   @prop()
   countryId: number;
}

export class User{
 @prop()
 userId: string;

 @prop()
 password: string;

@prop()
securityQuestion: string;

 @prop()
 userInfo : UserInfo;

}

component.ts:

export class UserInfoComponent implements OnInit {

    userInfoFormGroup: FormGroup

    constructor(
        private formBuilder: RxFormBuilder
    ) { }

    ngOnInit() {
        this.userInfoFormGroup = this.formBuilder.formGroup(User);
    }
}

I want to use UserInfo object in the user model as a nested formgroup. How can I create nested formgroup?

Here is a stackblitz link : https://stackblitz.com/edit/rxweb-nested-formgroup-using-model

1

1 Answers

4
votes

According to an example from @rxweb/reactive-form-validators you should decorate you userInfo property with @propObject decorator:

import { propObject} from "@rxweb/reactive-form-validators";

export class User {
  ...

  @propObject(UserInfo)
  userInfo: UserInfo;
}

And also initialize FormGroup like:

ngOnInit() {
    let user = new User();
    user.userInfo = new UserInfo();
    this.userInfoFormGroup = this.formBuilder.formGroup(user);
}

Forked Stackblitz