2016-07-29 74 views
0

Hy guys我是Angular2和JS框架中的新手。我在官方网站上流动的教程,一直没能找到解决这个问题的方法。Angular 2 - 如果选中复选框,则需要字段验证

所以我有复选框,这是可选的,但如果复选框“检查”一个新的输入字段显示。这部分不是问题。问题是,我正在使用基于模式的验证,我不知道如何使这个新的输入字段只有在复选框被选中时才需要。

这是5月实施至今:

<form [formGroup]="form" (ngSubmit)="onSubmit()"> 

<!--{{form}}--> 

<div formGroupName="test"> 
    <div class="field"> 
     <div class="checkbox"> 
      <input type="checkbox" name="entryRecurring" value="" id="entryRecurring" formControlName="entryRecurring" /> 
      <label for="entryRecurring"> 
       <div class="checkbox_icon"></div> 
       Recurring Entry 
      </label> 
     </div> 
    </div> 

    <div *ngIf="form.value.test.entryRecurring"> 
     <div class="field"> 
      <label for="entryRecurringAmount">Repeat Amount</label> 
      <input type="text" name="entryRecurringAmount" value="" id="entryRecurringAmount" formControlName="entryRecurringAmount" /> 
     </div> 
    </div> 
</div> 

<div class="field last"> 
    <button name="submit" id="submit" class="btn btn_sushi" [disabled]="!form.valid">Submit</button> 
</div> 

import {Component, Input, OnInit, OnChanges} from '@angular/core'; 
    import { Validators } from '@angular/common'; 
    import { REACTIVE_FORM_DIRECTIVES, FormGroup, FormControl, FormBuilder } from '@angular/forms'; 
    import { FormMessages } from './../helpers/formMessages.component'; 
    import {EntriesService} from './entries.service';  
    import {ValidationService} from '../helpers/validation.service'; 
    import {Category, CategoryByType} from '../../mock/mock-categories'; 


    @Component({ 
     selector: 'entryForm', 
     templateUrl: 'app/components/entries/entriesEdit.template.html', 
     directives: [REACTIVE_FORM_DIRECTIVES, FormMessages], 
     providers: [EntriesService, ValidationService] 

    }) 
    export class EntriesEditComponent implements OnInit, OnChanges { 
     @Input() control: FormControl; 
     public form:FormGroup; 
     public submitted:boolean = false; 
     // private selectedId: number; 

     categories: Category[]; 
     categoriesSortedByType: CategoryByType[]; 

     constructor(
      private _fb:FormBuilder, 
      private _entriesService: EntriesService 
      // private _router: Router 
     ) { 

      this.form = this._fb.group({ 

       test: this._fb.group({ 
        entryRecurring: [''], 
        entryRecurringAmount: [''], 
       }) 
      }); 
     } 
onSubmit() { 
     this.submitted = true; 
     // console.log(this.form.value); 

     if (this.form.dirty && this.form.valid) { 
      this._entriesService.saveEntry(this.form.value); 
     } 
    } 
+0

我的同样的问题仍然没有解决或在github中关闭。但我知道方式在那里。希望有人会在这里很快回答... – micronyks

回答

0

你可以做,通过使用自定义的验证服务。

import {NgFormModel} from "angular2/common"; 
import {Component, Host} from 'angular2/core'; 

@Component({ 
    selector : 'validation-message', 
    template : ` 
     <span *ngIf="errorMessage !== null">{{errorMessage}}</span> 
    `, 
    inputs: ['controlName : field'], 
}) 



export class ControlMessages { 

    controlName : string; 

    constructor(@Host() private _formDir : NgFormModel){ 

    } 

    get errorMessage() : string { 
     let input = this._formDir.form.find(this.controlName); 
     let checkBx = this._formDir.form.find('checkBoxName'); 
     if(input.value.trim() === '' && checkBx.checked) { 
       return 'The input field is now required' 
     } 
     return null; 
    } 

} 

然后使用新的部件像波纹管

<div *ngIf="form.value.test.entryRecurring"> 
     <div class="field"> 
      <label for="entryRecurringAmount">Repeat Amount</label> 
      <input type="text" name="entryRecurringAmount" value="" id="entryRecurringAmount" ngControl="entryRecurringAmount" /> 
      <validation-message field="entryRecurringAmount"></validation-message> 
     </div> 
    </div> 

希望这有助于!

相关问题