fix(forms): fix min and max validator behavior on non-numbers

This commit is contained in:
Kara Erickson
2017-06-07 20:22:23 -07:00
committed by Alex Rickabaugh
parent 5a71df0cb3
commit a222c3e609
2 changed files with 46 additions and 14 deletions

View File

@ -64,11 +64,13 @@ export class Validators {
*/
static min(min: number): ValidatorFn {
return (control: AbstractControl): ValidationErrors | null => {
if (isEmptyInputValue(control.value)) {
if (isEmptyInputValue(control.value) || isEmptyInputValue(min)) {
return null; // don't validate empty values to allow optional controls
}
const value = parseFloat(control.value);
return isNaN(value) || value < min ? {'min': {'min': min, 'actual': control.value}} : null;
// Controls with NaN values after parsing should be treated as not having a
// minimum, per the HTML forms spec: https://www.w3.org/TR/html5/forms.html#attr-input-min
return !isNaN(value) && value < min ? {'min': {'min': min, 'actual': control.value}} : null;
};
}
@ -77,11 +79,13 @@ export class Validators {
*/
static max(max: number): ValidatorFn {
return (control: AbstractControl): ValidationErrors | null => {
if (isEmptyInputValue(control.value)) {
if (isEmptyInputValue(control.value) || isEmptyInputValue(max)) {
return null; // don't validate empty values to allow optional controls
}
const value = parseFloat(control.value);
return isNaN(value) || value > max ? {'max': {'max': max, 'actual': control.value}} : null;
// Controls with NaN values after parsing should be treated as not having a
// maximum, per the HTML forms spec: https://www.w3.org/TR/html5/forms.html#attr-input-max
return !isNaN(value) && value > max ? {'max': {'max': max, 'actual': control.value}} : null;
};
}