所以现在我正在使用Angular6反应表单模块来创建表单,除了验证模块。
form = new FormGroup({ groupOne: new FormGroup({ example: new FormControl('', Validation.required) }) })
当然,这是一个非常基本的实现,但是当验证方法开始堆积时,尤其是在一个大的表单上,这会变得非常冗长,有没有更谨慎的方法来处理这个问题,即将验证/规则与控制器解耦?
您可以使用FormBuilder功能。这减少了定义FormGroup所需的代码量。
constructor(private fb: FormBuilder) {} this.productForm = this.fb.group({ productName: ['', [Validators.required, Validators.minLength(3), Validators.maxLength(50)]], productCode: ['', Validators.required], starRating: ['', NumberValidators.range(1, 5)], tags: this.fb.array([]), description: '' });
fb
我建议用图书馆 ng-form-rules great documentation , examples video that show a bunch of its functionality .
ng-form-rules
check out their README