如何在角度 6 的单选按钮中执行验证?

ash*_*rki 3 javascript angular angular6

实际上我有两种类型的输入类型:一种是普通文本,另一种是单选类型。我想在输入提交按钮之前验证这两个字段。

在此处输入图片说明

对于这个普通的输入字段,我进行了验证,但我不知道如何为单选按钮做。我想验证用户是否没有选择任何单选按钮。

<form  name="form" #f="ngForm" (ngSubmit)="f.form.valid && onSubmit()" novalidate class="feedback-form">
         <div class="col-md-12">

            <div class="form-group col-md-4">
               <label for="selectionDate">Selection Date</label>
               <input type="text" 
               id="selectionDate"
               class="form-control"
                name="selectionDate"
                placeholder="Please enter the date" 
               [(ngModel)]="selection.selectionDate"
                #selectionDate="ngModel"
                [ngClass]="{ 'is-invalid': f.submitted && selectionDate.invalid }"
                 required
                  />

             <div *ngIf="f.submitted && selectionDate.invalid" class="invalid-input">
            <!-- individual validation errors -->
            <div *ngIf="selectionDate.errors?.required">DateField is required</div>
          </div>

            </div>
            <div class="form-group col-md-4">
               <label for="selectedBy">Selected By</label>
               <br>
               <label class="radio-inline">
               <input type="radio" name="selectedBy" 
               [(ngModel)]="selection.selectedBy" value="Department">Department
               </label>
               <label class="radio-inline">
               <input type="radio" name="selectedBy"  
               [(ngModel)]="selection.selectedBy" value="Office">Office
               </label>
            </div>
         </div>
</form>
Run Code Online (Sandbox Code Playgroud)

mal*_*awi 5

这与您之前验证输入元素的方式相同

        <div class="form-group col-md-4">
           <label for="selectedBy">Selected By</label>
           <br>
           <label class="radio-inline">
           <input type="radio" name="selectedBy" 
           [(ngModel)]="selection.selectedBy" value="Department" required
                           #selectedBy="ngModel">Department
           </label>
           <label class="radio-inline">
           <input type="radio" name="selectedBy"  
           [(ngModel)]="selection.selectedBy" value="Office" required>Office
           </label>
        <div *ngIf="selectedBy.errors?.required">selectedBy is required</div>
      </div>
Run Code Online (Sandbox Code Playgroud)

演示