@@ -32,22 +32,6 @@ class MyApp extends StatelessWidget {
32
32
}
33
33
}
34
34
35
- final class _NonNegativeNum <T extends num >
36
- extends BaseElementaryValidator <T , T > {
37
- _NonNegativeNum () : super (ignoreErrorMessage: false );
38
-
39
- @override
40
- (bool , T ? ) transformValueIfValid (T value) {
41
- if (value >= 0 ) {
42
- return (true , value);
43
- }
44
- return (false , null );
45
- }
46
-
47
- @override
48
- String get translatedErrorText => 'We cannot have a negative age' ;
49
- }
50
-
51
35
/// Represents the home page of the application.
52
36
class NewAPIHomePage extends StatelessWidget {
53
37
/// Constructs a new instance of the [NewAPIHomePage] class.
@@ -67,14 +51,11 @@ class NewAPIHomePage extends StatelessWidget {
67
51
decoration: const InputDecoration (labelText: 'Age' ),
68
52
keyboardType: TextInputType .number,
69
53
autovalidateMode: AutovalidateMode .always,
70
- validator: FormBuilderValidators .compose ([
71
- ValidatorBuilder .required (and: < Validator <Object , num >> [
72
- ValidatorBuilder .numeric (
73
- errorText: 'La edad debe ser numérica.' ,
74
- and: < BaseElementaryValidator <num , dynamic >> [
75
- ValidatorBuilder .max (70 ),
76
- ])
77
- ]).validate,
54
+ validator:
55
+ FormBuilderValidators .compose (< FormFieldValidator <String >> [
56
+ req (
57
+ isNum (max (70 ), isNumMessage: 'La edad debe ser numérica.' ),
58
+ ),
78
59
79
60
/// Include your own custom `FormFieldValidator` function, if you want
80
61
/// Ensures positive values only. We could also have used `FormBuilderValidators.min( 0)` instead
@@ -87,7 +68,9 @@ class NewAPIHomePage extends StatelessWidget {
87
68
return null ;
88
69
}
89
70
]),
71
+
90
72
/*
73
+ // Original api
91
74
FormBuilderValidators.compose(<FormFieldValidator<String>>[
92
75
/// Makes this field required
93
76
FormBuilderValidators.required(),
@@ -119,16 +102,19 @@ class NewAPIHomePage extends StatelessWidget {
119
102
const InputDecoration (labelText: 'Age (better way to do)' ),
120
103
keyboardType: TextInputType .number,
121
104
autovalidateMode: AutovalidateMode .always,
122
- validator:
123
- // Equivalent to: required && numeric && max(70) && _NonNegativeNum
124
- ValidatorBuilder .required (and: < Validator <Object , num >> [
125
- ValidatorBuilder .numeric (
126
- errorText: 'La edad debe ser numérica.' ,
127
- and: < BaseElementaryValidator <num , dynamic >> [
128
- ValidatorBuilder .max (70 ),
129
- _NonNegativeNum <num >(),
130
- ])
131
- ]).validate,
105
+ validator: req (
106
+ isNum (
107
+ and ([
108
+ max (70 ),
109
+ (num value) {
110
+ if (value < 0 ) return 'We cannot have a negative age' ;
111
+
112
+ return null ;
113
+ },
114
+ ]),
115
+ isNumMessage: 'La edad debe ser numérica.' ,
116
+ ),
117
+ ),
132
118
),
133
119
134
120
// Required Validator
@@ -138,7 +124,7 @@ class NewAPIHomePage extends StatelessWidget {
138
124
prefixIcon: Icon (Icons .star),
139
125
),
140
126
// validator: FormBuilderValidators.required(),
141
- validator: ValidatorBuilder . required ().validate ,
127
+ validator: req ( null ) ,
142
128
autofillHints: const < String > [AutofillHints .name],
143
129
textInputAction: TextInputAction .next,
144
130
autovalidateMode: AutovalidateMode .always,
@@ -151,11 +137,11 @@ class NewAPIHomePage extends StatelessWidget {
151
137
),
152
138
keyboardType: TextInputType .number,
153
139
//validator: FormBuilderValidators.numeric(),
154
- validator : ValidatorBuilder . required (
155
- ignoreErrorMessage : true ,
156
- and : < Validator < Object , num > > [
157
- ValidatorBuilder . numeric ()
158
- ]).validate,
140
+ // To maintain the previous behavior, we use msg. Below, there is a more precise way of doing that.
141
+ validator : msg (
142
+ FormBuilderLocalizations .current.numericErrorText,
143
+ req ( isNum ( null ))),
144
+
159
145
autofillHints: const < String > [AutofillHints .oneTimeCode],
160
146
textInputAction: TextInputAction .next,
161
147
autovalidateMode: AutovalidateMode .always,
@@ -167,23 +153,23 @@ class NewAPIHomePage extends StatelessWidget {
167
153
),
168
154
keyboardType: TextInputType .number,
169
155
//validator: FormBuilderValidators.numeric(),
170
- validator: ValidatorBuilder .required (
171
- and: < Validator <Object , num >> [
172
- ValidatorBuilder .numeric ()
173
- ]).validate,
156
+ validator: req (isNum (null )),
174
157
autofillHints: const < String > [AutofillHints .oneTimeCode],
175
158
textInputAction: TextInputAction .next,
176
159
autovalidateMode: AutovalidateMode .always,
177
160
),
178
- /*
179
161
// Email Validator
180
162
TextFormField (
181
163
decoration: const InputDecoration (
182
164
labelText: 'Email Field' ,
183
165
prefixIcon: Icon (Icons .email),
184
166
),
185
167
keyboardType: TextInputType .emailAddress,
186
- validator: FormBuilderValidators.email(),
168
+ //validator: FormBuilderValidators.email(),
169
+ validator: msg (
170
+ FormBuilderLocalizations .current.emailErrorText,
171
+ req (email ()),
172
+ ),
187
173
autofillHints: const < String > [AutofillHints .email],
188
174
textInputAction: TextInputAction .next,
189
175
autovalidateMode: AutovalidateMode .always,
@@ -195,7 +181,11 @@ class NewAPIHomePage extends StatelessWidget {
195
181
prefixIcon: Icon (Icons .link),
196
182
),
197
183
keyboardType: TextInputType .url,
198
- validator: FormBuilderValidators.url(),
184
+ //validator: FormBuilderValidators.url(),
185
+ validator: msg (
186
+ FormBuilderLocalizations .current.urlErrorText,
187
+ req (url ()),
188
+ ),
199
189
autofillHints: const < String > [AutofillHints .url],
200
190
textInputAction: TextInputAction .next,
201
191
autovalidateMode: AutovalidateMode .always,
@@ -206,7 +196,11 @@ class NewAPIHomePage extends StatelessWidget {
206
196
labelText: 'Min Length Field' ,
207
197
prefixIcon: Icon (Icons .text_fields),
208
198
),
209
- validator: FormBuilderValidators.minLength(5),
199
+ //validator: FormBuilderValidators.minLength(5),
200
+ validator: msg (
201
+ FormBuilderLocalizations .current.minLengthErrorText (5 ),
202
+ req (minLength (5 )),
203
+ ),
210
204
textInputAction: TextInputAction .next,
211
205
autovalidateMode: AutovalidateMode .always,
212
206
),
@@ -216,7 +210,11 @@ class NewAPIHomePage extends StatelessWidget {
216
210
labelText: 'Max Length Field' ,
217
211
prefixIcon: Icon (Icons .text_fields),
218
212
),
219
- validator: FormBuilderValidators.maxLength(10),
213
+ //validator: FormBuilderValidators.maxLength(10),
214
+ validator: msg (
215
+ FormBuilderLocalizations .current.maxLengthErrorText (5 ),
216
+ req (maxLength (5 )),
217
+ ),
220
218
textInputAction: TextInputAction .next,
221
219
autovalidateMode: AutovalidateMode .always,
222
220
),
@@ -227,7 +225,11 @@ class NewAPIHomePage extends StatelessWidget {
227
225
prefixIcon: Icon (Icons .exposure_neg_1),
228
226
),
229
227
keyboardType: TextInputType .number,
230
- validator: FormBuilderValidators.min(10),
228
+ //validator: FormBuilderValidators.min(10),
229
+ validator: msg (
230
+ FormBuilderLocalizations .current.minErrorText (10 ),
231
+ req (isNum (min (10 ))),
232
+ ),
231
233
textInputAction: TextInputAction .next,
232
234
autovalidateMode: AutovalidateMode .always,
233
235
),
@@ -238,7 +240,11 @@ class NewAPIHomePage extends StatelessWidget {
238
240
prefixIcon: Icon (Icons .exposure_plus_1),
239
241
),
240
242
keyboardType: TextInputType .number,
241
- validator: FormBuilderValidators.max(100),
243
+ //validator: FormBuilderValidators.max(100),
244
+ validator: msg (
245
+ FormBuilderLocalizations .current.maxErrorText (100 ),
246
+ req (isNum (max (100 ))),
247
+ ),
242
248
textInputAction: TextInputAction .next,
243
249
autovalidateMode: AutovalidateMode .always,
244
250
),
@@ -248,7 +254,8 @@ class NewAPIHomePage extends StatelessWidget {
248
254
labelText: 'Equal Field' ,
249
255
prefixIcon: Icon (Icons .check),
250
256
),
251
- validator: FormBuilderValidators.equal('test'),
257
+ //validator: FormBuilderValidators.equal('test'),
258
+ validator: equal ('test' ),
252
259
textInputAction: TextInputAction .next,
253
260
autovalidateMode: AutovalidateMode .always,
254
261
),
@@ -258,7 +265,11 @@ class NewAPIHomePage extends StatelessWidget {
258
265
labelText: 'Contains "test"' ,
259
266
prefixIcon: Icon (Icons .search),
260
267
),
261
- validator: FormBuilderValidators.contains('test'),
268
+ //validator: FormBuilderValidators.contains('test'),
269
+ validator: msg (
270
+ FormBuilderLocalizations .current.containsErrorText ('test' ),
271
+ req (contains ('test' )),
272
+ ),
262
273
textInputAction: TextInputAction .next,
263
274
autovalidateMode: AutovalidateMode .always,
264
275
),
@@ -268,8 +279,12 @@ class NewAPIHomePage extends StatelessWidget {
268
279
labelText: 'Match Pattern' ,
269
280
prefixIcon: Icon (Icons .pattern),
270
281
),
271
- validator:
272
- FormBuilderValidators.match(RegExp(r'^[a-zA-Z0-9]+$')),
282
+ //validator:
283
+ // FormBuilderValidators.match(RegExp(r'^[a-zA-Z0-9]+$')),
284
+ validator: msg (
285
+ FormBuilderLocalizations .current.matchErrorText,
286
+ req (match (RegExp (r'^[a-zA-Z0-9]+$' ))),
287
+ ),
273
288
textInputAction: TextInputAction .next,
274
289
autovalidateMode: AutovalidateMode .always,
275
290
),
@@ -280,7 +295,11 @@ class NewAPIHomePage extends StatelessWidget {
280
295
prefixIcon: Icon (Icons .computer),
281
296
),
282
297
keyboardType: TextInputType .number,
283
- validator: FormBuilderValidators.ip(),
298
+ // validator: FormBuilderValidators.ip(),
299
+ validator: msg (
300
+ FormBuilderLocalizations .current.ipErrorText,
301
+ req (ip ()),
302
+ ),
284
303
textInputAction: TextInputAction .next,
285
304
autovalidateMode: AutovalidateMode .always,
286
305
),
@@ -290,7 +309,11 @@ class NewAPIHomePage extends StatelessWidget {
290
309
labelText: 'UUID Field' ,
291
310
prefixIcon: Icon (Icons .code),
292
311
),
293
- validator: FormBuilderValidators.uuid(),
312
+ //validator: FormBuilderValidators.uuid(),
313
+ validator: msg (
314
+ FormBuilderLocalizations .current.uuidErrorText,
315
+ req (uuid ()),
316
+ ),
294
317
textInputAction: TextInputAction .next,
295
318
autovalidateMode: AutovalidateMode .always,
296
319
),
@@ -301,7 +324,11 @@ class NewAPIHomePage extends StatelessWidget {
301
324
prefixIcon: Icon (Icons .credit_card),
302
325
),
303
326
keyboardType: TextInputType .number,
304
- validator: FormBuilderValidators.creditCard(),
327
+ //validator: FormBuilderValidators.creditCard(),
328
+ validator: msg (
329
+ FormBuilderLocalizations .current.creditCardErrorText,
330
+ req (creditCard ()),
331
+ ),
305
332
autofillHints: const < String > [AutofillHints .creditCardNumber],
306
333
textInputAction: TextInputAction .next,
307
334
autovalidateMode: AutovalidateMode .always,
@@ -313,7 +340,11 @@ class NewAPIHomePage extends StatelessWidget {
313
340
prefixIcon: Icon (Icons .phone),
314
341
),
315
342
keyboardType: TextInputType .phone,
316
- validator: FormBuilderValidators.phoneNumber(),
343
+ //validator: FormBuilderValidators.phoneNumber(),
344
+ validator: msg (
345
+ FormBuilderLocalizations .current.phoneErrorText,
346
+ req (phoneNumber ()),
347
+ ),
317
348
autofillHints: const < String > [AutofillHints .telephoneNumber],
318
349
textInputAction: TextInputAction .next,
319
350
autovalidateMode: AutovalidateMode .always,
@@ -325,7 +356,8 @@ class NewAPIHomePage extends StatelessWidget {
325
356
prefixIcon: Icon (Icons .lock),
326
357
),
327
358
obscureText: true ,
328
- validator: FormBuilderValidators.password(),
359
+ //validator: FormBuilderValidators.password(),
360
+ validator: req (password ()),
329
361
autofillHints: const < String > [AutofillHints .password],
330
362
textInputAction: TextInputAction .done,
331
363
autovalidateMode: AutovalidateMode .always,
@@ -337,18 +369,19 @@ class NewAPIHomePage extends StatelessWidget {
337
369
prefixIcon: Icon (Icons .calendar_today),
338
370
),
339
371
keyboardType: TextInputType .number,
372
+ /*
340
373
validator:
341
374
FormBuilderValidators.compose(<FormFieldValidator<String>>[
342
375
FormBuilderValidators.required(),
343
376
FormBuilderValidators.numeric(),
344
377
FormBuilderValidators.min(0),
345
378
FormBuilderValidators.max(120),
346
379
]),
380
+ */
381
+ validator: req (isNum (and ([min (0 ), max (120 )]))),
347
382
textInputAction: TextInputAction .done,
348
383
autovalidateMode: AutovalidateMode .always,
349
384
),
350
-
351
- */
352
385
],
353
386
),
354
387
),
0 commit comments