Skip to content

Commit

Permalink
fix(forms): Add float number support for min and max validator (angul…
Browse files Browse the repository at this point in the history
…ar#42223)

Added float number support for minValidator and maxValidator

Closes angular#42215

PR Close angular#42223
  • Loading branch information
iRealNirmal authored and umairhm committed May 28, 2021
1 parent c022275 commit 640a52b
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/forms/src/directives/validators.ts
Expand Up @@ -178,7 +178,7 @@ export class MaxValidator extends AbstractValidatorDirective implements OnChange
/** @internal */
inputName = 'max';
/** @internal */
normalizeInput = (input: string): number => parseInt(input, 10);
normalizeInput = (input: string): number => parseFloat(input);
/** @internal */
createValidator = (max: number): ValidatorFn => maxValidator(max);
/**
Expand Down Expand Up @@ -238,7 +238,7 @@ export class MinValidator extends AbstractValidatorDirective implements OnChange
/** @internal */
inputName = 'min';
/** @internal */
normalizeInput = (input: string): number => parseInt(input, 10);
normalizeInput = (input: string): number => parseFloat(input);
/** @internal */
createValidator = (min: number): ValidatorFn => minValidator(min);
/**
Expand Down
68 changes: 68 additions & 0 deletions packages/forms/test/reactive_integration_spec.ts
Expand Up @@ -2563,6 +2563,40 @@ const ValueAccessorB = createControlValueAccessor('[cva-b]');
expect(form.controls.pin.errors).toEqual({max: {max: 1, actual: 2}});
});

it('should validate max for float number', () => {
const fixture = initTest(getComponent(dir));
const control = new FormControl(10.25);
fixture.componentInstance.control = control;
fixture.componentInstance.form = new FormGroup({'pin': control});
fixture.componentInstance.max = 10.35;
fixture.detectChanges();

const input = fixture.debugElement.query(By.css('input')).nativeElement;
const form = fixture.componentInstance.form;

expect(input.value).toEqual('10.25');
expect(form.valid).toBeTruthy();
expect(form.controls.pin.errors).toBeNull();

input.value = 10.15;
dispatchEvent(input, 'input');
expect(form.value).toEqual({pin: 10.15});
expect(form.valid).toBeTruthy();
expect(form.controls.pin.errors).toBeNull();

fixture.componentInstance.max = 10.05;
fixture.detectChanges();

expect(form.valid).toBeFalse();
expect(form.controls.pin.errors).toEqual({max: {max: 10.05, actual: 10.15}});

input.value = 10.01;
dispatchEvent(input, 'input');
expect(form.value).toEqual({pin: 10.01});
expect(form.valid).toBeTruthy();
expect(form.controls.pin.errors).toBeNull();
});

it('should apply max validation when control value is defined as a string', () => {
const fixture = initTest(getComponent(dir));
const control = new FormControl('5');
Expand Down Expand Up @@ -2615,6 +2649,40 @@ const ValueAccessorB = createControlValueAccessor('[cva-b]');
expect(form.controls.pin.errors).toEqual({min: {min: 5, actual: 2}});
});

it('should validate min for float number', () => {
const fixture = initTest(getComponent(dir));
const control = new FormControl(10.25);
fixture.componentInstance.control = control;
fixture.componentInstance.form = new FormGroup({'pin': control});
fixture.componentInstance.max = 10.50;
fixture.componentInstance.min = 10.25;
fixture.detectChanges();

const input = fixture.debugElement.query(By.css('input')).nativeElement;
const form = fixture.componentInstance.form;

expect(input.value).toEqual('10.25');
expect(form.valid).toBeTruthy();
expect(form.controls.pin.errors).toBeNull();

input.value = 10.35;
dispatchEvent(input, 'input');
expect(form.value).toEqual({pin: 10.35});
expect(form.valid).toBeTruthy();
expect(form.controls.pin.errors).toBeNull();

fixture.componentInstance.min = 10.40;
fixture.detectChanges();
expect(form.valid).toBeFalse();
expect(form.controls.pin.errors).toEqual({min: {min: 10.40, actual: 10.35}});

input.value = 10.45;
dispatchEvent(input, 'input');
expect(form.value).toEqual({pin: 10.45});
expect(form.valid).toBeTruthy();
expect(form.controls.pin.errors).toBeNull();
});

it('should apply min validation when control value is defined as a string', () => {
const fixture = initTest(getComponent(dir));
const control = new FormControl('5');
Expand Down
67 changes: 67 additions & 0 deletions packages/forms/test/template_integration_spec.ts
Expand Up @@ -1540,6 +1540,40 @@ import {NgModelCustomComp, NgModelCustomWrapper} from './value_accessor_integrat
expect(form.controls.max.errors).toBeNull();
}));

it('should validate max for float number', fakeAsync(() => {
const fixture = initTest(NgModelMaxValidator);
fixture.componentInstance.max = 10.25;
fixture.detectChanges();
tick();

const input = fixture.debugElement.query(By.css('input')).nativeElement;
const form = fixture.debugElement.children[0].injector.get(NgForm);

input.value = '';
dispatchEvent(input, 'input');
fixture.detectChanges();
expect(form.valid).toEqual(true);
expect(form.controls.max.errors).toBeNull();

input.value = 10.25;
dispatchEvent(input, 'input');
fixture.detectChanges();
expect(form.valid).toEqual(true);
expect(form.controls.max.errors).toBeNull();

input.value = 10.15;
dispatchEvent(input, 'input');
fixture.detectChanges();
expect(form.valid).toEqual(true);
expect(form.controls.max.errors).toBeNull();

input.value = 10.35;
dispatchEvent(input, 'input');
fixture.detectChanges();
expect(form.valid).toEqual(false);
expect(form.controls.max.errors).toEqual({max: {max: 10.25, actual: 10.35}});
}));

it('should apply max validation when control value is defined as a string', fakeAsync(() => {
const fixture = initTest(NgModelMaxValidator);
fixture.componentInstance.max = 10;
Expand Down Expand Up @@ -1617,6 +1651,39 @@ import {NgModelCustomComp, NgModelCustomWrapper} from './value_accessor_integrat
expect(form.controls.min.errors).toEqual({min: {min: 10, actual: 9}});
}));

it('should validate min for float number', fakeAsync(() => {
const fixture = initTest(NgModelMinValidator);
fixture.componentInstance.min = 10.25;
fixture.detectChanges();
tick();

const input = fixture.debugElement.query(By.css('input')).nativeElement;
const form = fixture.debugElement.children[0].injector.get(NgForm);

input.value = '';
dispatchEvent(input, 'input');
fixture.detectChanges();
expect(form.valid).toEqual(true);
expect(form.controls.min.errors).toBeNull();

input.value = 10.35;
dispatchEvent(input, 'input');
fixture.detectChanges();
expect(form.valid).toEqual(true);
expect(form.controls.min.errors).toBeNull();

input.value = 10.25;
dispatchEvent(input, 'input');
fixture.detectChanges();
expect(form.valid).toEqual(true);
expect(form.controls.min.errors).toBeNull();

input.value = 10.15;
dispatchEvent(input, 'input');
fixture.detectChanges();
expect(form.valid).toEqual(false);
expect(form.controls.min.errors).toEqual({min: {min: 10.25, actual: 10.15}});
}));
it('should apply min validation when control value is defined as a string', fakeAsync(() => {
const fixture = initTest(NgModelMinValidator);
fixture.componentInstance.min = 10;
Expand Down
28 changes: 28 additions & 0 deletions packages/forms/test/validators_spec.ts
Expand Up @@ -61,6 +61,20 @@ describe('Validators', () => {
expect(Validators.min(2)(new FormControl('1'))).toEqual({'min': {'min': 2, 'actual': '1'}});
});

it('should not error on small float number validation', () => {
expect(Validators.min(1.20)(new FormControl(1.25))).toBeNull();
});

it('should not error on equal float values', () => {
expect(Validators.min(1.25)(new FormControl(1.25))).toBeNull();
});

it('should return a validation error on big values', () => {
expect(Validators.min(1.25)(new FormControl(1.20))).toEqual({
'min': {'min': 1.25, 'actual': 1.20}
});
});

it('should not error on big values', () => {
expect(Validators.min(2)(new FormControl(3))).toBeNull();
});
Expand Down Expand Up @@ -105,6 +119,20 @@ describe('Validators', () => {
expect(Validators.max(2)(new FormControl('aaa'))).toBeNull();
});

it('should not error on small float number validation', () => {
expect(Validators.max(1.20)(new FormControl(1.15))).toBeNull();
});

it('should not error on equal float values', () => {
expect(Validators.max(1.25)(new FormControl(1.25))).toBeNull();
});

it('should return a validation error on big values', () => {
expect(Validators.max(1.25)(new FormControl(1.30))).toEqual({
'max': {'max': 1.25, 'actual': 1.30}
});
});

it('should return a validation error on big values', () => {
expect(Validators.max(2)(new FormControl(3))).toEqual({'max': {'max': 2, 'actual': 3}});
});
Expand Down

0 comments on commit 640a52b

Please sign in to comment.