Skip to content
This repository was archived by the owner on May 20, 2023. It is now read-only.

Commit 8fdfece

Browse files
TedSandernshahan
authored andcommitted
Use a number formatter for the upperbound/lowerbound validator to format the value in the error shown to the user. This allows the numerical values in the error to be internationalized.
PiperOrigin-RevId: 176450480
1 parent b3b1b94 commit 8fdfece

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

lib/material_input/material_number_validators.dart

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ class CheckNonNegativeValidator implements Validator {
6464
const Provider(NG_VALIDATORS, useExisting: LowerBoundValidator, multi: true)
6565
])
6666
class LowerBoundValidator implements Validator {
67+
final NumberFormat _numberFormat;
68+
69+
LowerBoundValidator(@Optional() NumberFormat format)
70+
: _numberFormat = format ?? new NumberFormat.decimalPattern();
71+
6772
/// Smallest allowed value.
6873
@Input()
6974
num lowerBound;
@@ -73,12 +78,13 @@ class LowerBoundValidator implements Validator {
7378
if (control.value == null || lowerBound == null) return null;
7479
assert(control.value is Comparable, 'Value needs to be Comparable');
7580
if (control.value < lowerBound) {
76-
return {numberBelowLowerBoundErrorKey: numberIsTooSmallMsg(lowerBound)};
81+
final lowerText = _numberFormat.format(lowerBound);
82+
return {numberBelowLowerBoundErrorKey: numberIsTooSmallMsg(lowerText)};
7783
}
7884
return null;
7985
}
8086

81-
static String numberIsTooSmallMsg(num _lowerBound) =>
87+
static String numberIsTooSmallMsg(String _lowerBound) =>
8288
Intl.message('Enter a number $_lowerBound or greater',
8389
name: 'LowerBoundValidator_numberIsTooSmallMsg',
8490
args: [_lowerBound],
@@ -92,6 +98,11 @@ class LowerBoundValidator implements Validator {
9298
const Provider(NG_VALIDATORS, useExisting: UpperBoundValidator, multi: true)
9399
])
94100
class UpperBoundValidator implements Validator {
101+
final NumberFormat _numberFormat;
102+
103+
UpperBoundValidator(@Optional() NumberFormat format)
104+
: _numberFormat = format ?? new NumberFormat.decimalPattern();
105+
95106
/// Largest allowed value.
96107
@Input()
97108
num upperBound;
@@ -101,12 +112,13 @@ class UpperBoundValidator implements Validator {
101112
if (control.value == null) return null; // Handled by accessor validator
102113
assert(control.value is Comparable, 'Value needs to be Comparable');
103114
if (control.value > upperBound) {
104-
return {numberAboveUpperBoundErrorKey: numberIsTooLargeMsg(upperBound)};
115+
final upperText = _numberFormat.format(upperBound);
116+
return {numberAboveUpperBoundErrorKey: numberIsTooLargeMsg(upperText)};
105117
}
106118
return null;
107119
}
108120

109-
static String numberIsTooLargeMsg(num _upperBound) =>
121+
static String numberIsTooLargeMsg(String _upperBound) =>
110122
Intl.message('Enter a number $_upperBound or smaller',
111123
name: 'UpperBoundValidator_numberIsTooLargeMsg',
112124
args: [_upperBound],

pubspec.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@ author: Dart Team <[email protected]>
88
environment:
99
sdk: '>=1.24.0 <2.0.0'
1010
dependencies:
11-
angular: 5.0.0-alpha+1
11+
angular: 5.0.0-alpha
1212
angular_forms: 1.0.1-alpha
1313
collection: ^1.14.0
1414
fixnum: ^0.10.5
1515
intl: '>=0.14.0 <0.16.0'
1616
js: ^0.6.1
1717
logging: ^0.11.2
1818
meta: ^1.0.4
19-
observable: '>=0.17.0+1 <0.21.0'
19+
observable: '>=0.14.0+1 <0.21.0'
2020
perf_api: ^0.1.0
21-
quiver: '>=0.24.0 <0.26.0'
21+
quiver: '>=0.22.0 <0.26.0'
2222
sass_builder: ^1.0.1
2323
uuid: ^0.5.3
2424
transformers:

0 commit comments

Comments
 (0)