0

我有反应形式:

  protected _valueForm = this._formBuilder.group({
    valueCaption: '',
  });

  protected _elementForm = this._formBuilder.group({
    caption: ['', Validators.required],
    required: [false]
  });

然后ngOnInit()有一个条件并且_elementForm正在改变:

      this._elementForm = this._formBuilder.group({
      caption: ['', Validators.required],
      required: false,
      valueForm: this._valueForm,
      values: this._formBuilder.array([])
    },
    {validator: FormElementValidator});

验证器:

export const FormElementValidator = (control: AbstractControl): ValidationErrors => {
  const values = (control.get('values') as FormArray).value;
  return (!!values.length) ? null : {noValues: 'T_FORM_VALUES_ARE_REQUIRED'};
}

而且效果很好,但是如果为空elementForm,则无效。valueCaption为什么会这样,我该如何解决?

4

1 回答 1

0

如果您不希望它是必需的,为什么要添加 Validators.required 只需执行类似的操作

protected _elementForm = this._formBuilder.group({
    caption: ['']    
});
于 2017-06-01T09:52:02.370 回答