Skip to content
This repository has been archived by the owner on May 7, 2021. It is now read-only.

fix(inlineinput): float value validation for user input #2816

Merged
merged 8 commits into from
Dec 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/app/widgets/inlineinput/inlineinput.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export class InlineInputComponent implements OnInit {
});
} else {
this.errorMessage = `Invalid value for the field type ${this.type}`;
this.inputField.nativeElement.focus();
}
}

Expand All @@ -83,12 +84,17 @@ export class InlineInputComponent implements OnInit {
}
}

isFloat(val) {
let x = parseFloat(val);
return typeof x === 'number' && Number.isFinite(x) && x >= -2147483648 && x <= 2147483648;
}

validateValue(value) {
if (this.type === 'integer') {
return /^\d+$/.test(value);
}
if (this.type === 'float') {
return /^-?\d*(\.\d+)?$/.test(value);
return this.isFloat(value);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@abhinandan13jan This method is still failing because value is of type string. value needs to be parsed first for this to work.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sahil143 I was trying not to alter any preexisting functionalities. Now, I've added a .focus() method for the validation failure case. Can you please check again if it looks ok now?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@joshuawilson I've updated using sematic release format

}
return true;
}
Expand Down Expand Up @@ -161,6 +167,9 @@ export class InlineInputComponent implements OnInit {
break;
}
}
if (this.editing && this.type == 'float' && keycode == 13) {
this.saveClick();
}
if (this.isNotValid) {
this.errorMessage = `This is a ${this.type} field`;
}
Expand Down
41 changes: 41 additions & 0 deletions src/app/widgets/inlineinput/inlineinput.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { async } from '@angular/core/testing';
import { InlineInputComponent } from './inlineinput.component';

describe('Unit Test :: Inline Input Float value', () => {
let comp = new InlineInputComponent();
beforeEach(async(() => {
comp.type = 'float';
}));

it('Should return true for input 2234.523 in float range', () => {
expect(comp.validateValue('2234.523')).toBe(true);
});

it('Should return true for input 1.2 in float range', () => {
expect(comp.validateValue('1.2')).toBe(true);
});

it('Should return true for input 223456.334 in float range', () => {
expect(comp.validateValue('223456.334')).toBe(true);
});

it('Should return true for input 45 in float range', () => {
expect(comp.validateValue('45')).toBe(true);
});

it('Should return false for input -34567812322.523 outside float range', () => {
expect(comp.validateValue('-34567812322.523')).toBe(false);
});

it('Should return false for input 345678123225.23678 outside float range', () => {
expect(comp.validateValue('345678123225.23678')).toBe(false);
});

it('Should return false for input stringtest outside float range', () => {
expect(comp.validateValue('stringtest')).toBe(false);
});

it('Should return false for input 11111111111111111.51 outside float range', () => {
expect(comp.validateValue('11111111111111111.51')).toBe(false);
});
});