2016-09-21 61 views
0

我有一个窗体有两个日期字段,我想验证开始日期早于结束日期。非常直截了当。 我选择使用反应形式而不是模板来实现表单。我正在使用rc5。我搜索了很多,并看到了模板模型的指令(我现在选择不去追求)的示例,但我看到的反应形式的示例似乎不再适用。反应式(基于模型)形式交叉字段验证2角度

你会觉得下面这个例子会工作,但它实际上并不:Cross field validation in Angular2

searchlog.component.html

<form [formGroup]="logsearchForm" > 
<p-panel> 
    <div class="ui-grid ui-grid-responsive ui-grid-pad ui-fluid"> 
    <div class="ui-grid-row ui-fluid"> 
     <div class="ui-grid-col-1">Search Logs</div> 
     <div class="ui-grid-col-4 ui-fluid"> 
     <input type="text" pInputText formControlName="searchString" placeholder="Enter text you want to search"/> 
     </div> 
     <div> 
     <button pButton type="submit" (ngSubmit) = "searchForLogs(logSearchForm.value, logSearchForm.valid)" label="Search" [disabled]="!logsearchForm.valid"></button> 
     </div> 
    </div> 
    <p-panel> 
     <div class="ui-grid-row ui-fluid"> 
     <div class="ui-grid-col-1">Applications:</div> 
     <div class="ui-grid-col-2"> 
      <p-dropdown [options]="applications" formControlName="selectedApp"> 
      </p-dropdown> 
     </div> 

     <div class="ui-grid-col-1">Users:</div> 
     <div class="ui-grid-col-2"> 
      <p-dropdown [options]="users" formControlName="selectedUser"></p-dropdown> 
     </div> 

     </div> 
     <div class="ui-grid-row ui-fluid"> 
     <div class="ui-grid-col-1">Start Time:</div> 
     <div class="ui-grid-col-2"> 
      <p-calendar formControlName="startDate" dateFormat="mm/dd/yy" timeFormat="HH:mm" timeControlType="select" 
         [horizontalTimeControls]="true"></p-calendar> 

     </div> 

     <div class="ui-grid-col-1">End Time</div> 
     <div class="ui-grid-col-2"> 
      <p-calendar formControlName="endDate" dateFormat="mm/dd/yy" timeFormat="HH:mm" timeControlType="select" 
         [horizontalTimeControls]="true">> 
      </p-calendar> 
     </div> 

     </div> 
    </p-panel> 
    </div> 
</p-panel> 
</form> 

searchlog.component.ts

.... initControls() { this.searchStringControl = new FormControl('', Validators.required); this.applicationsControl = new FormControl(''); this.usersControl = new FormControl(''); this.startDateControl = new FormControl(this.startDate); this.endDateControl = new FormControl(this.endDate); } .... ngOnInit() { this.startDate = this.getCurrentTimeAsString(); this.endDate = this.getTime24HoursAgoAsString(); this.initControls(); this.logsearchForm = this.fb.group({ 'searchString': this.searchStringControl, 'selectedApp': this.applicationsControl, 'selectedUser': this.usersControl, 'period': this.fb.group({ 'startDate': this.startDateControl, 'endDate': this.endDateControl }) }) } .... 

甚至没有添加验证新FormGroup我得到以下在控制台中出错。

browser_adapter.js:84ORIGINAL EXCEPTION: Cannot find control with name: 'startDate' 

任何想法如何实现这一目标?你会认为这将是一个非常普遍的问题,但我能找到的所有答案都不再适用于Angular 2.

+0

我相信这是太晚了你,但无论如何:你需要把在[formGroup] =“周期” div围绕该表单组内的控件。 – Spock

回答

0

我发现的一种方法适用于我(在源代码中查找)是:

this.logsearchForm = new FormGroup({ 
    'searchString': new FormControl('', Validators.required), 
    'selectedApp': new FormControl(''), 
    'selectedUser': new FormControl(''), 
    'startDate': new FormControl(this.startDate, Validators.required), 
    'endDate': new FormControl(this.endDate) 

}, {}, this.periodValidator); 

和下面的验证(使用日期操作moment.js)

periodValidator(formGroup:FormGroup) { 
    let stDt = moment(formGroup.value.startDate, "MM/DD/YYYY HH:mm"); 
    let endDt = moment(formGroup.value.endDate, "MM/DD/YYYY HH:mm"); 
    if (stDt < endDt) { 
     return null; 
    } 
    else { 
     return {'failedDateValidation': true} 
    } 

    } 
+0

这适用于一次对整个表单进行验证,但是如果您需要一个定制验证器来处理依赖于其他字段的字段,该怎么办?举例来说,一个'confirmPassword'字段只有在它与主'password'中的内容匹配并且'password'字段非空时才有效'字段验证器获取FormControl,而不是包含所有表单值的整个FormGroup。 –