1
1
import 'package:flutter/material.dart' ;
2
-
3
2
import 'package:form_builder_validators/form_builder_validators.dart' ;
4
3
5
4
import 'utils/validators.dart' ;
@@ -23,17 +22,15 @@ class FormBuilderValidators {
23
22
}
24
23
25
24
/// [FormFieldValidator] that requires the field have a non-empty value.
26
- static FormFieldValidator <T > required < T > (
27
- BuildContext context, {
25
+ static FormFieldValidator <T > required < T > ({
28
26
String ? errorText,
29
27
}) {
30
28
return (T ? valueCandidate) {
31
29
if (valueCandidate == null ||
32
30
(valueCandidate is String && valueCandidate.trim ().isEmpty) ||
33
31
(valueCandidate is Iterable && valueCandidate.isEmpty) ||
34
32
(valueCandidate is Map && valueCandidate.isEmpty)) {
35
- return errorText ??
36
- FormBuilderLocalizations .of (context).requiredErrorText;
33
+ return errorText ?? FormBuilderLocalizations .current.requiredErrorText;
37
34
}
38
35
return null ;
39
36
};
@@ -42,31 +39,27 @@ class FormBuilderValidators {
42
39
/// [FormFieldValidator] that requires the field's value be equal to the
43
40
/// provided value.
44
41
static FormFieldValidator <T > equal <T >(
45
- BuildContext context,
46
42
Object value, {
47
43
String ? errorText,
48
44
}) =>
49
45
(valueCandidate) => valueCandidate != value
50
- ? errorText ??
51
- FormBuilderLocalizations .of (context).equalErrorText (value)
46
+ ? errorText ?? FormBuilderLocalizations .current.equalErrorText (value)
52
47
: null ;
53
48
54
49
/// [FormFieldValidator] that requires the field's value be not equal to
55
50
/// the provided value.
56
51
static FormFieldValidator <T > notEqual <T >(
57
- BuildContext context,
58
52
Object value, {
59
53
String ? errorText,
60
54
}) =>
61
55
(valueCandidate) => valueCandidate == value
62
56
? errorText ??
63
- FormBuilderLocalizations .of (context) .notEqualErrorText (value)
57
+ FormBuilderLocalizations .current .notEqualErrorText (value)
64
58
: null ;
65
59
66
60
/// [FormFieldValidator] that requires the field's value to be greater than
67
61
/// (or equal) to the provided number.
68
62
static FormFieldValidator <T > min <T >(
69
- BuildContext context,
70
63
num min, {
71
64
bool inclusive = true ,
72
65
String ? errorText,
@@ -80,7 +73,7 @@ class FormBuilderValidators {
80
73
81
74
if (number != null && (inclusive ? number < min : number <= min)) {
82
75
return errorText ??
83
- FormBuilderLocalizations .of (context) .minErrorText (min);
76
+ FormBuilderLocalizations .current .minErrorText (min);
84
77
}
85
78
}
86
79
return null ;
@@ -90,7 +83,6 @@ class FormBuilderValidators {
90
83
/// [FormFieldValidator] that requires the field's value to be less than
91
84
/// (or equal) to the provided number.
92
85
static FormFieldValidator <T > max <T >(
93
- BuildContext context,
94
86
num max, {
95
87
bool inclusive = true ,
96
88
String ? errorText,
@@ -104,7 +96,7 @@ class FormBuilderValidators {
104
96
105
97
if (number != null && (inclusive ? number > max : number >= max)) {
106
98
return errorText ??
107
- FormBuilderLocalizations .of (context) .maxErrorText (max);
99
+ FormBuilderLocalizations .current .maxErrorText (max);
108
100
}
109
101
}
110
102
return null ;
@@ -114,7 +106,6 @@ class FormBuilderValidators {
114
106
/// [FormFieldValidator] that requires the length of the field's value to be
115
107
/// greater than or equal to the provided minimum length.
116
108
static FormFieldValidator <T > minLength <T >(
117
- BuildContext context,
118
109
int minLength, {
119
110
bool allowEmpty = false ,
120
111
String ? errorText,
@@ -129,15 +120,14 @@ class FormBuilderValidators {
129
120
if (valueCandidate is Iterable ) valueLength = valueCandidate.length;
130
121
return valueLength < minLength && (! allowEmpty || valueLength > 0 )
131
122
? errorText ??
132
- FormBuilderLocalizations .of (context) .minLengthErrorText (minLength)
123
+ FormBuilderLocalizations .current .minLengthErrorText (minLength)
133
124
: null ;
134
125
};
135
126
}
136
127
137
128
/// [FormFieldValidator] that requires the length of the field's value to be
138
129
/// less than or equal to the provided maximum length.
139
130
static FormFieldValidator <T > maxLength <T >(
140
- BuildContext context,
141
131
int maxLength, {
142
132
String ? errorText,
143
133
}) {
@@ -151,24 +141,22 @@ class FormBuilderValidators {
151
141
if (valueCandidate is Iterable ) valueLength = valueCandidate.length;
152
142
return null != valueCandidate && valueLength > maxLength
153
143
? errorText ??
154
- FormBuilderLocalizations .of (context) .maxLengthErrorText (maxLength)
144
+ FormBuilderLocalizations .current .maxLengthErrorText (maxLength)
155
145
: null ;
156
146
};
157
147
}
158
148
159
149
/// [FormFieldValidator] that requires the field's value to be a valid email address.
160
- static FormFieldValidator <String > email (
161
- BuildContext context, {
150
+ static FormFieldValidator <String > email ({
162
151
String ? errorText,
163
152
}) =>
164
153
(valueCandidate) =>
165
154
true == valueCandidate? .isNotEmpty && ! isEmail (valueCandidate! .trim ())
166
- ? errorText ?? FormBuilderLocalizations .of (context) .emailErrorText
155
+ ? errorText ?? FormBuilderLocalizations .current .emailErrorText
167
156
: null ;
168
157
169
158
/// [FormFieldValidator] that requires the field's value to be a valid url.
170
- static FormFieldValidator <String > url (
171
- BuildContext context, {
159
+ static FormFieldValidator <String > url ({
172
160
String ? errorText,
173
161
List <String > protocols = const ['http' , 'https' , 'ftp' ],
174
162
bool requireTld = true ,
@@ -185,72 +173,64 @@ class FormBuilderValidators {
185
173
allowUnderscore: allowUnderscore,
186
174
hostWhitelist: hostWhitelist,
187
175
hostBlacklist: hostBlacklist)
188
- ? errorText ?? FormBuilderLocalizations .of (context) .urlErrorText
176
+ ? errorText ?? FormBuilderLocalizations .current .urlErrorText
189
177
: null ;
190
178
191
179
/// [FormFieldValidator] that requires the field's value to match the provided regex pattern.
192
180
static FormFieldValidator <String > match (
193
- BuildContext context,
194
181
String pattern, {
195
182
String ? errorText,
196
183
}) =>
197
184
(valueCandidate) => true == valueCandidate? .isNotEmpty &&
198
185
! RegExp (pattern).hasMatch (valueCandidate! )
199
- ? errorText ?? FormBuilderLocalizations .of (context) .matchErrorText
186
+ ? errorText ?? FormBuilderLocalizations .current .matchErrorText
200
187
: null ;
201
188
202
189
/// [FormFieldValidator] that requires the field's value to be a valid number.
203
- static FormFieldValidator <String > numeric (
204
- BuildContext context, {
190
+ static FormFieldValidator <String > numeric ({
205
191
String ? errorText,
206
192
}) =>
207
193
(valueCandidate) => true == valueCandidate? .isNotEmpty &&
208
194
null == num .tryParse (valueCandidate! )
209
- ? errorText ?? FormBuilderLocalizations .of (context) .numericErrorText
195
+ ? errorText ?? FormBuilderLocalizations .current .numericErrorText
210
196
: null ;
211
197
212
198
/// [FormFieldValidator] that requires the field's value to be a valid integer.
213
- static FormFieldValidator <String > integer (
214
- BuildContext context, {
199
+ static FormFieldValidator <String > integer ({
215
200
String ? errorText,
216
201
int ? radix,
217
202
}) =>
218
203
(valueCandidate) => true == valueCandidate? .isNotEmpty &&
219
204
null == int .tryParse (valueCandidate! , radix: radix)
220
- ? errorText ?? FormBuilderLocalizations .of (context) .integerErrorText
205
+ ? errorText ?? FormBuilderLocalizations .current .integerErrorText
221
206
: null ;
222
207
223
208
/// [FormFieldValidator] that requires the field's value to be a valid credit card number.
224
- static FormFieldValidator <String > creditCard (
225
- BuildContext context, {
209
+ static FormFieldValidator <String > creditCard ({
226
210
String ? errorText,
227
211
}) =>
228
- (valueCandidate) =>
229
- true == valueCandidate? .isNotEmpty && ! isCreditCard (valueCandidate! )
230
- ? errorText ??
231
- FormBuilderLocalizations .of (context).creditCardErrorText
232
- : null ;
212
+ (valueCandidate) => true == valueCandidate? .isNotEmpty &&
213
+ ! isCreditCard (valueCandidate! )
214
+ ? errorText ?? FormBuilderLocalizations .current.creditCardErrorText
215
+ : null ;
233
216
234
217
/// [FormFieldValidator] that requires the field's value to be a valid IP address.
235
218
/// * [version] is a `String` or an `int` .
236
- static FormFieldValidator <String > ip (
237
- BuildContext context, {
219
+ static FormFieldValidator <String > ip ({
238
220
int ? version,
239
221
String ? errorText,
240
222
}) =>
241
223
(valueCandidate) =>
242
224
true == valueCandidate? .isNotEmpty && ! isIP (valueCandidate! , version)
243
- ? errorText ?? FormBuilderLocalizations .of (context) .ipErrorText
225
+ ? errorText ?? FormBuilderLocalizations .current .ipErrorText
244
226
: null ;
245
227
246
228
/// [FormFieldValidator] that requires the field's value to be a valid date string.
247
- static FormFieldValidator <String > dateString (
248
- BuildContext context, {
229
+ static FormFieldValidator <String > dateString ({
249
230
String ? errorText,
250
231
}) =>
251
- (valueCandidate) =>
252
- true == valueCandidate? .isNotEmpty && ! isDate (valueCandidate! )
253
- ? errorText ??
254
- FormBuilderLocalizations .of (context).dateStringErrorText
255
- : null ;
232
+ (valueCandidate) => true == valueCandidate? .isNotEmpty &&
233
+ ! isDate (valueCandidate! )
234
+ ? errorText ?? FormBuilderLocalizations .current.dateStringErrorText
235
+ : null ;
256
236
}
0 commit comments