Skip to content

Commit 5dcdcf5

Browse files
committed
feat: Use FormBuilderLocalizations.current instead of FormBuilderLocalizations.of(context) for localization
avoids passing context to validation functions
1 parent 0ef753f commit 5dcdcf5

File tree

3 files changed

+66
-98
lines changed

3 files changed

+66
-98
lines changed

packages/form_builder_validators/example/lib/main.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class _MyHomePageState extends State<MyHomePage> {
5353
children: <Widget>[
5454
TextFormField(
5555
decoration: const InputDecoration(labelText: 'Name'),
56-
validator: FormBuilderValidators.required(context),
56+
validator: FormBuilderValidators.required(),
5757
autovalidateMode: AutovalidateMode.always,
5858
),
5959

@@ -64,17 +64,17 @@ class _MyHomePageState extends State<MyHomePage> {
6464
autovalidateMode: AutovalidateMode.always,
6565
validator: FormBuilderValidators.compose([
6666
/// Makes this field required
67-
FormBuilderValidators.required(context),
67+
FormBuilderValidators.required(),
6868

6969
/// Ensures the value entered is numeric - with custom error message
70-
FormBuilderValidators.numeric(context,
70+
FormBuilderValidators.numeric(
7171
errorText: 'La edad debe ser numérica.'),
7272

7373
/// Sets a maximum value of 70
74-
FormBuilderValidators.max(context, 70),
74+
FormBuilderValidators.max(70),
7575

7676
/// Include your own custom `FormFieldValidator` function, if you want
77-
/// Ensures positive values only. We could also have used `FormBuilderValidators.min(context, 0)` instead
77+
/// Ensures positive values only. We could also have used `FormBuilderValidators.min( 0)` instead
7878
(val) {
7979
final number = int.tryParse(val);
8080
if (number == null) return null;

packages/form_builder_validators/lib/src/form_builder_validators.dart

Lines changed: 29 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import 'package:flutter/material.dart';
2-
32
import 'package:form_builder_validators/form_builder_validators.dart';
43

54
import 'utils/validators.dart';
@@ -23,17 +22,15 @@ class FormBuilderValidators {
2322
}
2423

2524
/// [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>({
2826
String? errorText,
2927
}) {
3028
return (T? valueCandidate) {
3129
if (valueCandidate == null ||
3230
(valueCandidate is String && valueCandidate.trim().isEmpty) ||
3331
(valueCandidate is Iterable && valueCandidate.isEmpty) ||
3432
(valueCandidate is Map && valueCandidate.isEmpty)) {
35-
return errorText ??
36-
FormBuilderLocalizations.of(context).requiredErrorText;
33+
return errorText ?? FormBuilderLocalizations.current.requiredErrorText;
3734
}
3835
return null;
3936
};
@@ -42,31 +39,27 @@ class FormBuilderValidators {
4239
/// [FormFieldValidator] that requires the field's value be equal to the
4340
/// provided value.
4441
static FormFieldValidator<T> equal<T>(
45-
BuildContext context,
4642
Object value, {
4743
String? errorText,
4844
}) =>
4945
(valueCandidate) => valueCandidate != value
50-
? errorText ??
51-
FormBuilderLocalizations.of(context).equalErrorText(value)
46+
? errorText ?? FormBuilderLocalizations.current.equalErrorText(value)
5247
: null;
5348

5449
/// [FormFieldValidator] that requires the field's value be not equal to
5550
/// the provided value.
5651
static FormFieldValidator<T> notEqual<T>(
57-
BuildContext context,
5852
Object value, {
5953
String? errorText,
6054
}) =>
6155
(valueCandidate) => valueCandidate == value
6256
? errorText ??
63-
FormBuilderLocalizations.of(context).notEqualErrorText(value)
57+
FormBuilderLocalizations.current.notEqualErrorText(value)
6458
: null;
6559

6660
/// [FormFieldValidator] that requires the field's value to be greater than
6761
/// (or equal) to the provided number.
6862
static FormFieldValidator<T> min<T>(
69-
BuildContext context,
7063
num min, {
7164
bool inclusive = true,
7265
String? errorText,
@@ -80,7 +73,7 @@ class FormBuilderValidators {
8073

8174
if (number != null && (inclusive ? number < min : number <= min)) {
8275
return errorText ??
83-
FormBuilderLocalizations.of(context).minErrorText(min);
76+
FormBuilderLocalizations.current.minErrorText(min);
8477
}
8578
}
8679
return null;
@@ -90,7 +83,6 @@ class FormBuilderValidators {
9083
/// [FormFieldValidator] that requires the field's value to be less than
9184
/// (or equal) to the provided number.
9285
static FormFieldValidator<T> max<T>(
93-
BuildContext context,
9486
num max, {
9587
bool inclusive = true,
9688
String? errorText,
@@ -104,7 +96,7 @@ class FormBuilderValidators {
10496

10597
if (number != null && (inclusive ? number > max : number >= max)) {
10698
return errorText ??
107-
FormBuilderLocalizations.of(context).maxErrorText(max);
99+
FormBuilderLocalizations.current.maxErrorText(max);
108100
}
109101
}
110102
return null;
@@ -114,7 +106,6 @@ class FormBuilderValidators {
114106
/// [FormFieldValidator] that requires the length of the field's value to be
115107
/// greater than or equal to the provided minimum length.
116108
static FormFieldValidator<T> minLength<T>(
117-
BuildContext context,
118109
int minLength, {
119110
bool allowEmpty = false,
120111
String? errorText,
@@ -129,15 +120,14 @@ class FormBuilderValidators {
129120
if (valueCandidate is Iterable) valueLength = valueCandidate.length;
130121
return valueLength < minLength && (!allowEmpty || valueLength > 0)
131122
? errorText ??
132-
FormBuilderLocalizations.of(context).minLengthErrorText(minLength)
123+
FormBuilderLocalizations.current.minLengthErrorText(minLength)
133124
: null;
134125
};
135126
}
136127

137128
/// [FormFieldValidator] that requires the length of the field's value to be
138129
/// less than or equal to the provided maximum length.
139130
static FormFieldValidator<T> maxLength<T>(
140-
BuildContext context,
141131
int maxLength, {
142132
String? errorText,
143133
}) {
@@ -151,24 +141,22 @@ class FormBuilderValidators {
151141
if (valueCandidate is Iterable) valueLength = valueCandidate.length;
152142
return null != valueCandidate && valueLength > maxLength
153143
? errorText ??
154-
FormBuilderLocalizations.of(context).maxLengthErrorText(maxLength)
144+
FormBuilderLocalizations.current.maxLengthErrorText(maxLength)
155145
: null;
156146
};
157147
}
158148

159149
/// [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({
162151
String? errorText,
163152
}) =>
164153
(valueCandidate) =>
165154
true == valueCandidate?.isNotEmpty && !isEmail(valueCandidate!.trim())
166-
? errorText ?? FormBuilderLocalizations.of(context).emailErrorText
155+
? errorText ?? FormBuilderLocalizations.current.emailErrorText
167156
: null;
168157

169158
/// [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({
172160
String? errorText,
173161
List<String> protocols = const ['http', 'https', 'ftp'],
174162
bool requireTld = true,
@@ -185,72 +173,64 @@ class FormBuilderValidators {
185173
allowUnderscore: allowUnderscore,
186174
hostWhitelist: hostWhitelist,
187175
hostBlacklist: hostBlacklist)
188-
? errorText ?? FormBuilderLocalizations.of(context).urlErrorText
176+
? errorText ?? FormBuilderLocalizations.current.urlErrorText
189177
: null;
190178

191179
/// [FormFieldValidator] that requires the field's value to match the provided regex pattern.
192180
static FormFieldValidator<String> match(
193-
BuildContext context,
194181
String pattern, {
195182
String? errorText,
196183
}) =>
197184
(valueCandidate) => true == valueCandidate?.isNotEmpty &&
198185
!RegExp(pattern).hasMatch(valueCandidate!)
199-
? errorText ?? FormBuilderLocalizations.of(context).matchErrorText
186+
? errorText ?? FormBuilderLocalizations.current.matchErrorText
200187
: null;
201188

202189
/// [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({
205191
String? errorText,
206192
}) =>
207193
(valueCandidate) => true == valueCandidate?.isNotEmpty &&
208194
null == num.tryParse(valueCandidate!)
209-
? errorText ?? FormBuilderLocalizations.of(context).numericErrorText
195+
? errorText ?? FormBuilderLocalizations.current.numericErrorText
210196
: null;
211197

212198
/// [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({
215200
String? errorText,
216201
int? radix,
217202
}) =>
218203
(valueCandidate) => true == valueCandidate?.isNotEmpty &&
219204
null == int.tryParse(valueCandidate!, radix: radix)
220-
? errorText ?? FormBuilderLocalizations.of(context).integerErrorText
205+
? errorText ?? FormBuilderLocalizations.current.integerErrorText
221206
: null;
222207

223208
/// [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({
226210
String? errorText,
227211
}) =>
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;
233216

234217
/// [FormFieldValidator] that requires the field's value to be a valid IP address.
235218
/// * [version] is a `String` or an `int`.
236-
static FormFieldValidator<String> ip(
237-
BuildContext context, {
219+
static FormFieldValidator<String> ip({
238220
int? version,
239221
String? errorText,
240222
}) =>
241223
(valueCandidate) =>
242224
true == valueCandidate?.isNotEmpty && !isIP(valueCandidate!, version)
243-
? errorText ?? FormBuilderLocalizations.of(context).ipErrorText
225+
? errorText ?? FormBuilderLocalizations.current.ipErrorText
244226
: null;
245227

246228
/// [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({
249230
String? errorText,
250231
}) =>
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;
256236
}

0 commit comments

Comments
 (0)