NGXS Forms - 如何处理嵌套表单组件

发布于 2025-01-14 01:56:37 字数 823 浏览 0 评论 0原文

我有一个包含多个步骤的用户配置文件表单,即一个向导。提交/保存时,必须将整个用户个人资料表单提交到 API 我正在使用 NGXS 和 NGXS 表格。

目前,我已将 UI 实现为单个 Angular Reactive Forms 和单个状态属性 form,该模型是 API 期望的类型。每个步骤都有一个组件,但它们都使用相同的表单。 这种方法的一个大问题是我无法检测步骤上的字段是否有效,我只能检测整个表单是否有效。

为了解决这个问题,我想我必须创建多个 Angular 表单。但是,我无法将它们全部绑定到同一个状态属性form,因为在每个步骤中我只有属性的子集。

因此,我现在的想法是在我的状态中为每个步骤创建一个表单属性,每个步骤都有自己的用于存储属性的接口。然后,我可以创建一个选择器,它将返回我可以提交给 API 的整个对象。

目前在代码中:

form: {
  model: UserProfileDto,
  ....
}

提案:

personalDetailsForm: {
model: PersonalDetailsModel, 
},
workDetailsForm: {
model: WorkDetailsModel
}

@Selector(ProfileDetails)
profileDetails(state: ProfileState) {
  return {...state.workDetailsForm.model, ...state.personalDetailsForm.model}
}

这是可行的方法还是有更简单的方法?

I've got a user profile form with multiple steps, i.e. a wizard. On Submit/Save, the entire user profile form has to be submitted to an API
I'm using NGXS and NGXS Forms.

At the moment I have implemented the UI as a single Angular Reactive Forms and a single state property form, the model is of the type the API expects. There is a component for each step, but they all use the same form.
The big problem with this approach is that I can't detect whether the fields on a step are valid or not, I can only detect if the entire form is valid.

To solve this, I think I have to create multiple Angular forms. However, I can't bind them all to the same state property form, as on each step I only have a subset of properties.

So, my thinking now is to create a form property in my state for each step, each with its own interface for the properties it stores. Then, I can create a selector that will return the entire object I can submit to the API.

In code, currently:

form: {
  model: UserProfileDto,
  ....
}

Proposal:

personalDetailsForm: {
model: PersonalDetailsModel, 
},
workDetailsForm: {
model: WorkDetailsModel
}

@Selector(ProfileDetails)
profileDetails(state: ProfileState) {
  return {...state.workDetailsForm.model, ...state.personalDetailsForm.model}
}

Is this the way to go or is there an easier approach?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

痴情 2025-01-21 01:56:37

这是一个非常意见问题,但我可以分享我的想法。

在父组件中,我将为每个步骤创建单独的表单:

  • 它有助于为每个步骤编写验证规则;
  • 您可以检查每个步骤中的形式是否有效/无效
  • ,如果您将其存储在父组件中,您可以编写在步骤之间切换的逻辑(例如,如果当前步骤无效,则不允许转到另一个步骤)
export class Component {

  ngOnInit(): void {
    this.workDetailsForm = this.buildWorkForm();
    this.personalDetailsForm = this.buildPersonalForm();


    // and through merge + valueChanges you can get the last object at any time
    merge(
      this.workDetailsForm.valueChanges,
      this.personalDetailsForm.valuesChanges  
    )
    .subscribe(() => {
      this.profileDetails = {
        ...this.workDetailsForm.value, 
        ...this.personalDetailsForm.value
      };
    });

    // here you can check, that the whole form is valid
    merge(
      this.workDetailsForm.statusChanges,
      this.personalDetailsForm.statusChanges  
    )
    .subscribe(() => {
      this.isFormValid = this.workDetailsForm.valid && this.personalDetailsForm.valid;
    });
  }


}


<app-step-work-details
 [form]="workDetailsForm"> </app-step-work-details>

<app-step-personal-details
 [form]="personalDetailsForm"> </app-step-personal-details>

It's very opinion question, but I can share my thoughts.

In parent component I will create separate form for every step:

  • it helps to write validation rules for every step;
  • you can check, is valid/invalid form in every step
  • in case, if you store it in parent component, you can write logic for switching between steps(e.g. not allow to go to another step in case of current step is invalid)
export class Component {

  ngOnInit(): void {
    this.workDetailsForm = this.buildWorkForm();
    this.personalDetailsForm = this.buildPersonalForm();


    // and through merge + valueChanges you can get the last object at any time
    merge(
      this.workDetailsForm.valueChanges,
      this.personalDetailsForm.valuesChanges  
    )
    .subscribe(() => {
      this.profileDetails = {
        ...this.workDetailsForm.value, 
        ...this.personalDetailsForm.value
      };
    });

    // here you can check, that the whole form is valid
    merge(
      this.workDetailsForm.statusChanges,
      this.personalDetailsForm.statusChanges  
    )
    .subscribe(() => {
      this.isFormValid = this.workDetailsForm.valid && this.personalDetailsForm.valid;
    });
  }


}


<app-step-work-details
 [form]="workDetailsForm"> </app-step-work-details>

<app-step-personal-details
 [form]="personalDetailsForm"> </app-step-personal-details>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文