使用数组中的不同Validations创建反应式表单数组

时间:2017-09-16 10:11:55

标签: angular angular-reactive-forms reactive-forms

我最近开始使用Angular4反应表单,我想基于下面的Structure

创建一个表单数组
"ContactPoints":
[{"ContactPointID":33,"EntityID":9,"System":"phone","Value":"1234567890","Use":"work","Rank":1,"InUseStart":"2017-07-14T00:00:00","InUseEnd":"2017-10-22T00:00:00"},
{"ContactPointID":34,"EntityID":9,"System":"phone","Value":"1234567890","Use":"home","Rank":1,"InUseStart":"2017-07-14T00:00:00","InUseEnd":"2017-10-22T00:00:00"},
{"ContactPointID":35,"EntityID":9,"System":"fax","Value":"1234567890","Use":"work","Rank":1,"InUseStart":"2017-07-14T00:00:00","InUseEnd":"2017-10-22T00:00:00"},
{"ContactPointID":36,"EntityID":9,"System":"email","Value":"kishore@abc.com","Use":"work","Rank":1,"InUseStart":"2017-07-21T00:00:00","InUseEnd":"2017-10-29T00:00:00"},
{"ContactPointID":37,"EntityID":9,"System":"email","Value":"kishore@abc.com","Use":"home","Rank":1,"InUseStart":"2017-07-21T00:00:00","InUseEnd":"2017-10-29T00:00:00"}],

下面是HTML语法,我需要在值和系统中获取相应的输入值,使用将是基于系统的硬编码值和验证,任何实现这一点的建议都将非常有帮助

 <div class="col-md-4" formArrayName="ContactPoints">
                <div class="form-group form-group-default  " formGroupName=0>
                    <label class="control-label">Main Number</label>
                    <input type="text" class="form-control" formControlName="Value">
                </div>
            </div>

<div class="col-md-4" formArrayName="ContactPoints">
                    <div class="form-group form-group-default  " formGroupName=1>
                        <label class="control-label">Main Number</label>
                        <input type="text" class="form-control" formControlName="Value">
                    </div>
                </div>

1 个答案:

答案 0 :(得分:0)

您可以使用动态表单https://angular.io/guide/dynamic-form来实现此目的。将每种输入类型(在您的情况下系统: - 电话,电子邮件,传真)作为单独的组件并在组件中添加验证将帮助您实现您的方案。 请参阅:https://toddmotto.com/angular-dynamic-components-forms给出了如何实现这种动态形式的一个很好的例子