简体   繁体   English

根据反应形式角度中的条件生成表单控件

[英]Generating formcontrols based on condition in reactive forms angular

I have created a reactive form in angular application.我在角度应用程序中创建了一个反应形式。 Currently there are set of fields shown on the left-hand side of the screen.目前,屏幕左侧显示了一组字段。 When the user selects the radio button, another set of similar fields need to be show on the right当用户选择单选按钮时,需要在右侧显示另一组类似的字段

At the moment the declaration of the fields is like this目前字段的声明是这样的

this.formGroup = this.formBuilder.group({
  yesNoMultilingual: [{ value: false, disabled: this.readonly }],
  hasDifferentPrincipalAddress: [{ value: null, disabled: this.readonly }, Validators.required],
  differentPrincipalAddress: [
    { value: null, disabled: this.readonly },
    [conditionalValidator(() => this.form.hasDifferentPrincipalAddress.value, requiredAddress)]
  ],
   
  hasOtherBusinessAddresses: [{ value: null, disabled: this.readonly }, Validators.required],
  otherAddresses: [{ value: null, disabled: this.readonly }, requiredIfValidator(() => this.form.hasOtherBusinessAddresses.value)]
});

I was thinking of declaring the fields with the alternate prefix below.我正在考虑使用下面的备用前缀声明字段。 I am not too sure if it is the right way to do it.我不太确定这是否是正确的方法。 Only when the user clicks yes the alternate fields need to come into existense.只有当用户单击是时,备用字段才需要存在。 Especially while saving the form and if the user does not select Yes, then the alternate fields shouldn't be generated as they would contain nulls and there is no point.尤其是在保存表单时,如果用户没有选择是,则不应生成替代字段,因为它们将包含空值并且没有意义。 Is there any conditional statement within this declaration or any better way to do this.此声明中是否有任何条件语句或任何更好的方法来执行此操作。

 this.formGroup = this.formBuilder.group({
      yesNoMultilingual: [{ value: false, disabled: this.readonly }],
      hasDifferentPrincipalAddress: [{ value: null, disabled: this.readonly }, Validators.required],
alternateHasDifferentPrincipalAddress: [{ value: null, disabled: this.readonly }, Validators.required],
      differentPrincipalAddress: [
        { value: null, disabled: this.readonly },
        [conditionalValidator(() => this.form.hasDifferentPrincipalAddress.value, requiredAddress)]
      ],
      alternateDifferentPrincipalAddress: [
        { value: null, disabled: this.readonly },
        [conditionalValidator(() => this.form.hasDifferentPrincipalAddress.value, requiredAddress)]
      ],
   
      hasOtherBusinessAddresses: [{ value: null, disabled: this.readonly }, Validators.required],
      alternateHasOtherBusinessAddresses: [{ value: null, disabled: this.readonly }, Validators.required],

      otherAddresses: [{ value: null, disabled: this.readonly }, requiredIfValidator(() => this.form.hasOtherBusinessAddresses.value)]
      alternateOtherAddresses: [{ value: null, disabled: this.readonly }, requiredIfValidator(() => this.form.hasOtherBusinessAddresses.value)]

    });

If you access your form.value it will not show disabled form values.如果您访问您的form.value它不会显示禁用的表单值。 For your use case here it would make sense to add these extra formControls & then conditionally disable / enable them based on the radio button's value you have mentioned.对于您的用例,添加这些额外的 formControls 然后根据您提到的单选按钮的值有条件地禁用/启用它们是有意义的。

You can listen to the changes in that value using valueChanges for that specific form control.您可以使用该特定表单控件的valueChanges来侦听该值的更改。

this.formGroup.get('yesNoMultilingual').valueChanges.subscribe(data => 
    // toggle enable / disable of your formControls
)

You could also nest these under a formGroup to make it easier to maintain their state.您还可以将它们嵌套在 formGroup 下,以便更轻松地维护它们的状态。 (As you can disable/enable the entire formGroup instead of individual controls!) (因为您可以禁用/启用整个 formGroup 而不是单个控件!)

this.formGroup = this.formBuilder.group({
   // existing formControls
   alternative: this.formBuilder.group({
    alternateDifferentPrincipalAddress: [null], etc etc
  }),
})

You could then toggle of the entire formGroup in one go.然后,您可以一次性切换整个 formGroup。

this.form.get('alternative').disable()
this.form.get('alternative').enable()

This would allow you wrap the extra part of your form in a conditional based on the formGroup.这将允许您将表单的额外部分包装在基于 formGroup 的条件中。

<ng-container *ngIf="formGroup.get('alternative').enabled" formGroupName="alternative">
    // extra form elements
</ng-container>

Alternatively you could add / remove the group but this seems overkill!或者,您可以添加/删除该组,但这似乎有点矫枉过正!

this.formGroup.removeControl('alternative')
this.formGroup.addControl('alternative', this.formBuilder.group({
     alternateDifferentPrincipalAddress: [null], etc etc
}))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM