验证器最小和最大数角4

时间:2017-05-05 15:33:06

标签: forms angular validation

是否有任何选项可以使验证器最小和最大数量,即0到200之间的数字?

对话框的结果-component.ts

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<select name="hold_reason">
<option value="">Select option</option>
<option value="Hold">Hold</option>
</select>


<div class="Hold reason">Hold reason
<select class="form-control" name="hold_reason">
<option value="">Select reason</option>
<option value="">hold</option>
</select>
</div>

对话框的result.component.html

import { Component, OnInit } from '@angular/core';
import { MdDialog, MdDialogRef } from '@angular/material';
import { FormGroup,FormControl,Validators,FormBuilder,  } from '@angular/forms';



@Component({
  selector: 'app-dialog-result',
  templateUrl: './dialog-result.component.html',
})

export class DialogResultComponent {


  form: FormGroup;
  name = new FormControl('',Validators.required);
  width = new FormControl('',Validators.required);
  height = new FormControl('',Validators.required);
  constructor(public dialogRef: MdDialogRef<DialogResultComponent>,private fb: FormBuilder) {

  }

  ngOnInit() {
  this.form = this.fb.group({
      'name':    this.name,
      'width':   this.width,
      'height':  this.height,
    });
}

  saveData(){
    this.dialogRef.close({name:this.name,width:this.width,height:this.height});
  }
}

最小值和最大值进入mdl-text-field

       <form [formGroup]="form">
     <h3>MineSweeperwix</h3>
      <div class="mdl-dialog__content">
                <p><mdl-textfield type="text" label="name" formControlName="name" floating-label autofocus></mdl-textfield></p>
                <mdl-textfield type="number" formControlName="width" min="0" max="350" label="width"   floating-label autofocus></mdl-textfield>
               <mdl-textfield type="number" formControlName="height" label="height" min="0" max="350" floating-label autofocus  error-msg="'Please provide a correct email!'"></mdl-textfield>
      </div>
      <div class="mdl-dialog__actions">
        <button mdl-button (click)="saveData()" mdl-button-type="raised" mdl-colored="primary" mdl-ripple [disabled]="!form.valid">Save</button>
        <button mdl-button (click)="dialogRef.close(dd)" mdl-button-type="raised" mdl-ripple>Cancel</button>
      </div>
   </form>

限制用户在范围之间写入数字,但是它让按下保存按钮,这不是我想要做的。我想用户可以在所有表​​格都有效时按下保存按钮。

我试图做的是

dialog.result.component.ts

min="0" max="200" floating-label autofocus

NumberValidators.module.ts

    import { Component, OnInit } from '@angular/core';
import { MdDialog, MdDialogRef } from '@angular/material';
import { FormGroup,FormControl,Validators,FormBuilder,ValidatorFn,AbstractControl  } from '@angular/forms';
import { NumberValidators } from '../validators/NumberValidators.module';


@Component({
  selector: 'app-dialog-result',
  templateUrl: './dialog-result.component.html',
})

export class DialogResultComponent {


  form: FormGroup;
  name = new FormControl('',Validators.required);
  width = new FormControl('',[Validators.required,NumberValidators.range(3,300)]);
  height = new FormControl('',[Validators.required,NumberValidators.range(3,300)]);
  constructor(public dialogRef: MdDialogRef<DialogResultComponent>,private fb: FormBuilder) {

  }

  ngOnInit() {
  this.form = this.fb.group({
      'name' :this.name,
      'width': this.width,
      'height':  this.height
    });
}

  saveData(){
    console.log(this.name.value);
    this.dialogRef.close({name:this.name.value,width:this.width.value,height:this.height.value});
  }

}

}

但它没有用,任何人都有任何建议吗?

2 个答案:

答案 0 :(得分:4)

您可以编写自己的数字验证器。像这样:

import { AbstractControl, ValidatorFn } from '@angular/forms';

export class NumberValidators {

    static range(min: number, max: number): ValidatorFn {
        return (c: AbstractControl): { [key: string]: boolean } | null => {
            if (c.value && (isNaN(c.value) || c.value < min || c.value > max)) {
                return { 'range': true };
            }
            return null;
        };
    }
}

答案 1 :(得分:3)

我找到的最简单的方法是使用验证器。请查看此参考链接:

https://angular.io/api/forms/Validators

static min(min: number): ValidatorFn
static max(max: number): ValidatorFn

我实现的完整代码示例如下:

参考component.ts

this.form = this.fb.group({
  'name' :new FormControl(null, Validators.compose([Validators.required])),
  'width':  new FormControl(null, Validators.compose([Validators.required, Validators.min(0), Validators.max(200)])),
  'height':  new FormControl(null, Validators.compose([Validators.required, Validators.min(0), Validators.max(200)]))
});

component.html

<div *ngIf="width.errors?.min || width.errors?.max" class="error">Width is not valid.</div>
<div *ngIf="height.errors?.min || height.errors?.max" class="error">Height is not valid.</div>