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