Angular6 |禁用隐藏字段的表单验证

时间:2018-08-23 13:56:45

标签: html angular forms validation hidden-field

背景

我有一个表格,可以让用户选择指定一个快速的时间范围或一个详细的时间范围(具有特定的开始/结束时间)-即用户可以选择仅填写 两分之一

他们希望选择的选项类型可以通过单击按钮进行切换。这些按钮只需切换指定div上的hidden参数

问题

我的问题是这两个字段(从技术上讲,所有3个字段的开始/结束时间都是一个广义div下的2个单独的表单组div),只有不是< / strong>隐藏-我不确定如何使用Angular 4/5/6(在此使用6)来实现这一点

我的无效表单触发器总是关闭,因为还需要隐藏字段

我的表单

<div class="card-block">
  <form #f="ngForm" (ngSubmit)="onSubmit(f)" novalidate>
    <div class="form-group">
      <label for="transactionType"> Request Type </label>
      <select 
      class="form-control" 
      [(ngModel)]=data.transactionType 
      name ="transactionType" 
      #transactionType="ngModel"
      required>
        <option value="something1Count">Something1</option>
        <option value="something2Count">Something2</option>
      </select> 
      <div *ngIf="transactionType.errors?.required && transactionType.touched" class="alert alert-danger">
        Please select a transaction type
      </div>
    </div>
    <br>
    <div class="form-group">
     <label>Timeframe Type</label> 
     <br>
        <button type="button" class="btn btn-light" (click)="quickTimeframe = false; detailedTimeframe = true;" ng-selected="!quickTimeFrame"> Quick </button>
        <button type="button" class="btn btn-light" (click)="detailedTimeframe = false; quickTimeframe = true;" ng-selected="!detailedTimeframe"> Detailed </button>
    </div>
    <div class="form-group" [hidden]="quickTimeframe">
      <label for="timeframe">Timeframe</label>
      <select 
      class="form-control" 
      [(ngModel)]=data.timeframe 
      name ="timeframe"
      #timeframe="ngModel"
      required>
        <option value="15">15 Minutes</option>
        <option value="30">30 Minutes</option>
        <option value="45">45 Minutes</option>
        <option value="60">60 Minutes</option>
      </select>
      <div *ngIf=" !quickTimeframe && timeframe.errors?.required && timeframe.touched" class="alert alert-danger">
        Please select a timeframe
      </div>
    </div>

      <div class="detailed" [hidden]="detailedTimeframe">
          <div class="form-group">
              <label for="startTime">Start of Time Range</label>
                  <input 
                  type="datetime-local"
                  class="form-control"
                  [(ngModel)]="data.startTime"
                  #startTime="ngModel"
                  name="startTime"
                  required
                  >
              <div *ngIf="!detailedTimeframe && startTime.errors?.required && startTime.touched" class="alert alert-danger">
                Please select a start-time
              </div>
            </div>

            <div class="form-group">
                <label for="endTime">End of Time Range</label>
                <input
                type="datetime-local"
                class="form-control"
                [(ngModel)]="data.endTime"
                #endTime="ngModel"
                name="endTime"
                required
                >
              <div *ngIf="!detailedTimeframe && endTime.errors?.required && endTime.touched" class="alert alert-danger">
                Please select an end-time
              </div>
            </div>
      </div>
    <input type="submit" class="btn btn-primary btn-block" value="Submit">
  </form>
  </div>
</div>

TS文件

import { Component, OnInit } from '@angular/core';
import { Inputs } from '../../models/Inputs';
import { Router } from '@angular/router';
import {FlashMessagesService} from 'angular2-flash-messages';

@Component({
  selector: 'app-main-form',
  templateUrl: './main-form.component.html',
  styleUrls: ['./main-form.component.css']
})
export class MainFormComponent implements OnInit {

  data:Inputs = {
    transactionType:'',
    timeframe:null,
    startTime:'',
    endTime:''
  };

  quickTimeframe:boolean=true;
  detailedTimeframe:boolean=true;
  buttonResult:any;

  constructor(public router:Router, public flashMessagesService:FlashMessagesService) { 

  }

  ngOnInit() {

  }

  onSubmit({value, valid}:{value:Inputs, valid:boolean}){

    console.log("Quick Time Flag: " +this.quickTimeframe);
    console.log("Detailed Time Flag: " +this.detailedTimeframe);

    if (valid && !this.quickTimeframe){
      console.log("Form is valid");
      //Stuff should happen
      console.log(value);
    } else if (valid && !this.detailedTimeframe) {
      console.log(this.data.startTime);
      //Other stuff should happen
    } 

    else {
      console.log("Form is not valid");
      this.flashMessagesService.show('Choose Valid Parameters', {cssClass:'alert-danger', timeout: 4000});
    }

  }

  radioClick() {
    console.log(this.buttonResult);
  }

}

报告输入界面(为完成起见)

 export interface Inputs {

    transactionType?:string;
    timeframe?:number;
    startTime?:string;
    endTime?:string;

}

1 个答案:

答案 0 :(得分:0)

* ng如果成功了

使用ngIf而不是隐藏,确保在我的场景中表单中不存在元素