Skip to content

Commit 18b2ee1

Browse files
committed
Done with errorText internationalization
1 parent d03c890 commit 18b2ee1

File tree

2 files changed

+80
-42
lines changed

2 files changed

+80
-42
lines changed

example/lib/sources/complete_form.dart

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ class CompleteFormState extends State<CompleteForm> {
8383
FormBuilderField(
8484
attribute: 'name',
8585
validator: FormBuilderValidators.compose([
86-
FormBuilderValidators.required(),
86+
FormBuilderValidators.required(context),
8787
]),
8888
initialValue: 'Algeria',
8989
builder: (FormFieldState<dynamic> field) {
@@ -193,7 +193,7 @@ class CompleteFormState extends State<CompleteForm> {
193193
FormBuilderSlider(
194194
attribute: 'slider',
195195
validator: FormBuilderValidators.compose([
196-
FormBuilderValidators.min(6),
196+
FormBuilderValidators.min(context, 6),
197197
]),
198198
onChanged: _onChanged,
199199
min: 0.0,
@@ -209,7 +209,7 @@ class CompleteFormState extends State<CompleteForm> {
209209
FormBuilderRangeSlider(
210210
attribute: 'range_slider',
211211
validator: FormBuilderValidators.compose(
212-
[FormBuilderValidators.min(6)]),
212+
[FormBuilderValidators.min(context, 6)]),
213213
onChanged: _onChanged,
214214
min: 0.0,
215215
max: 100.0,
@@ -245,6 +245,7 @@ class CompleteFormState extends State<CompleteForm> {
245245
),
246246
validator: FormBuilderValidators.compose([
247247
FormBuilderValidators.requireTrue(
248+
context,
248249
errorText:
249250
'You must accept terms and conditions to continue',
250251
),
@@ -259,9 +260,9 @@ class CompleteFormState extends State<CompleteForm> {
259260
onChanged: _onChanged,
260261
// valueTransformer: (text) => num.tryParse(text),
261262
validator: FormBuilderValidators.compose([
262-
FormBuilderValidators.required(),
263-
FormBuilderValidators.numeric(),
264-
FormBuilderValidators.max(70),
263+
FormBuilderValidators.required(context),
264+
FormBuilderValidators.numeric(context),
265+
FormBuilderValidators.max(context, 70),
265266
]),
266267
keyboardType: TextInputType.number,
267268
),
@@ -274,7 +275,7 @@ class CompleteFormState extends State<CompleteForm> {
274275
allowClear: true,
275276
hint: Text('Select Gender'),
276277
validator: FormBuilderValidators.compose(
277-
[FormBuilderValidators.required()]),
278+
[FormBuilderValidators.required(context)]),
278279
items: genderOptions
279280
.map((gender) => DropdownMenuItem(
280281
value: gender,
@@ -317,7 +318,7 @@ class CompleteFormState extends State<CompleteForm> {
317318
attribute: 'best_language',
318319
onChanged: _onChanged,
319320
validator: FormBuilderValidators.compose(
320-
[FormBuilderValidators.required()]),
321+
[FormBuilderValidators.required(context)]),
321322
options: ['Dart', 'Kotlin', 'Java', 'Swift', 'Objective-C']
322323
.map((lang) => FormBuilderFieldOption(
323324
value: lang,
@@ -450,7 +451,7 @@ class CompleteFormState extends State<CompleteForm> {
450451
},
451452
colorPickerType: ColorPickerType.ColorPicker,
452453
validator: FormBuilderValidators.compose([
453-
FormBuilderValidators.required(
454+
FormBuilderValidators.required(context,
454455
errorText: FormBuilderLocalizations.of(context)
455456
.requiredErrorText),
456457
]),
@@ -471,7 +472,7 @@ class CompleteFormState extends State<CompleteForm> {
471472
decoration: InputDecoration(
472473
border: OutlineInputBorder(), labelText: 'Country'),
473474
validator: FormBuilderValidators.compose([
474-
FormBuilderValidators.required(
475+
FormBuilderValidators.required(context,
475476
errorText: 'This field required.'),
476477
]),
477478
),
@@ -489,9 +490,9 @@ class CompleteFormState extends State<CompleteForm> {
489490
onChanged: _onChanged,
490491
priorityListByIsoCode: ['US'],
491492
validator: FormBuilderValidators.compose([
492-
FormBuilderValidators.numeric(
493+
FormBuilderValidators.numeric(context,
493494
errorText: 'Invalid phone number'),
494-
FormBuilderValidators.required(
495+
FormBuilderValidators.required(context,
495496
errorText: 'This field required'),
496497
]),
497498
),

lib/src/form_builder_validators.dart

Lines changed: 67 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'package:flutter/material.dart';
2+
import 'package:flutter_form_builder/flutter_form_builder.dart';
23
import 'package:validators/validators.dart';
34

45
class FormBuilderValidators {
@@ -18,29 +19,33 @@ class FormBuilderValidators {
1819
}
1920

2021
/// [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,
2325
}) {
2426
return (valueCandidate) {
2527
if (valueCandidate == null ||
2628
((valueCandidate is Iterable ||
2729
valueCandidate is String ||
2830
valueCandidate is Map) &&
2931
valueCandidate.length == 0)) {
30-
return errorText;
32+
return errorText ??
33+
FormBuilderLocalizations.of(context).requiredErrorText;
3134
}
3235
return null;
3336
};
3437
}
3538

3639
/// [FormFieldValidator] that requires the field's value be true.
3740
/// 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,
4044
}) {
4145
return (valueCandidate) {
4246
if (valueCandidate != true) {
43-
return errorText;
47+
return errorText ??
48+
FormBuilderLocalizations.of(context).requiredErrorText;
4449
}
4550
return null;
4651
};
@@ -49,6 +54,7 @@ class FormBuilderValidators {
4954
/// [FormFieldValidator] that requires the field's value to be greater than
5055
/// or equal to the provided number.
5156
static FormFieldValidator min(
57+
BuildContext context,
5258
num min, {
5359
String errorText,
5460
}) {
@@ -58,7 +64,8 @@ class FormBuilderValidators {
5864
(valueCandidate is String &&
5965
num.tryParse(valueCandidate) != null &&
6066
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);
6269
}
6370
return null;
6471
};
@@ -67,6 +74,7 @@ class FormBuilderValidators {
6774
/// [FormFieldValidator] that requires the field's value to be less than
6875
/// or equal to the provided number.
6976
static FormFieldValidator max(
77+
BuildContext context,
7078
num max, {
7179
String errorText,
7280
}) {
@@ -76,7 +84,8 @@ class FormBuilderValidators {
7684
(valueCandidate is String &&
7785
num.tryParse(valueCandidate) != null &&
7886
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);
8089
}
8190
}
8291
return null;
@@ -86,6 +95,7 @@ class FormBuilderValidators {
8695
/// [FormFieldValidator] that requires the length of the field's value to be
8796
/// greater than or equal to the provided minimum length.
8897
static FormFieldValidator minLength(
98+
BuildContext context,
8999
num minLength, {
90100
bool allowEmpty = false,
91101
String errorText,
@@ -94,7 +104,7 @@ class FormBuilderValidators {
94104
final valueLength = valueCandidate?.length ?? 0;
95105
if (valueLength < minLength && (!allowEmpty || valueLength > 0)) {
96106
return errorText ??
97-
'Value must have a length greater than or equal to $minLength';
107+
FormBuilderLocalizations.of(context).minLengthErrorText(minLength);
98108
}
99109
return null;
100110
};
@@ -103,33 +113,39 @@ class FormBuilderValidators {
103113
/// [FormFieldValidator] that requires the length of the field's value to be
104114
/// less than or equal to the provided maximum length.
105115
static FormFieldValidator maxLength(
116+
BuildContext context,
106117
num maxLength, {
107118
String errorText,
108119
}) {
109120
return (valueCandidate) {
110121
if (valueCandidate != null && valueCandidate.length > maxLength) {
111122
return errorText ??
112-
'Value must have a length less than or equal to $maxLength';
123+
FormBuilderLocalizations.of(context).maxLengthErrorText(maxLength);
113124
}
114125
return null;
115126
};
116127
}
117128

118129
/// [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,
121133
}) {
122134
return (valueCandidate) {
123135
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+
}
125140
}
126141
return null;
127142
};
128143
}
129144

130145
/// [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,
133149
List<String> protocols = const ['http', 'https', 'ftp'],
134150
bool requireTld = true,
135151
bool requireProtocol = false,
@@ -145,44 +161,57 @@ class FormBuilderValidators {
145161
requireProtocol: requireProtocol,
146162
allowUnderscore: allowUnderscore,
147163
hostWhitelist: hostWhitelist,
148-
hostBlacklist: hostBlacklist)) return errorText;
164+
hostBlacklist: hostBlacklist)) {
165+
return errorText ??
166+
FormBuilderLocalizations.of(context).urlErrorText;
167+
}
149168
}
150169
return null;
151170
};
152171
}
153172

154173
/// [FormFieldValidator] that requires the field's value to match the provided regex pattern.
155174
static FormFieldValidator pattern(
175+
BuildContext context,
156176
Pattern pattern, {
157-
String errorText = 'Value does not match pattern.',
177+
String errorText,
158178
}) {
159179
return (valueCandidate) {
160180
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+
}
162185
}
163186
return null;
164187
};
165188
}
166189

167190
/// [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,
170194
}) {
171195
return (valueCandidate) {
172196
if (num.tryParse(valueCandidate) == null && valueCandidate.isNotEmpty) {
173-
return errorText;
197+
return errorText ??
198+
FormBuilderLocalizations.of(context).numericErrorText;
174199
}
175200
return null;
176201
};
177202
}
178203

179204
/// [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,
182208
}) {
183209
return (valueCandidate) {
184210
if (valueCandidate != null && valueCandidate.isNotEmpty) {
185-
if (!isCreditCard(valueCandidate)) return errorText;
211+
if (!isCreditCard(valueCandidate)) {
212+
return errorText ??
213+
FormBuilderLocalizations.of(context).creditCardErrorText;
214+
}
186215
}
187216
return null;
188217
};
@@ -191,25 +220,33 @@ class FormBuilderValidators {
191220
/// [FormFieldValidator] that requires the field's value to be a valid IP address.
192221
/// * [version] is a String or an `int`.
193222
// ignore: non_constant_identifier_names
194-
static FormFieldValidator IP({
223+
static FormFieldValidator IP(
224+
BuildContext context, {
195225
dynamic version,
196-
String errorText = 'This field requires a valid IP.',
226+
String errorText,
197227
}) {
198228
return (valueCandidate) {
199229
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+
}
201234
}
202235
return null;
203236
};
204237
}
205238

206239
/// [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,
209243
}) {
210244
return (valueCandidate) {
211245
if (valueCandidate != null && valueCandidate.isNotEmpty) {
212-
if (!isDate(valueCandidate)) return errorText;
246+
if (!isDate(valueCandidate)) {
247+
return errorText ??
248+
FormBuilderLocalizations.of(context).dateStringErrorText;
249+
}
213250
}
214251
return null;
215252
};

0 commit comments

Comments
 (0)