Skip to content

Commit 286e5be

Browse files
add partially the isEqual validator
1 parent 9adfa88 commit 286e5be

File tree

7 files changed

+176
-17
lines changed

7 files changed

+176
-17
lines changed

example/lib/api_refactoring_main.dart renamed to example/lib/new_api_examples/api_refactoring_main.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import 'package:flutter/material.dart';
22
import 'package:flutter_localizations/flutter_localizations.dart';
33
import 'package:form_builder_validators/form_builder_validators.dart';
44

5-
import 'localization/intl/app_localizations.dart';
6-
import 'override_form_builder_localizations_en.dart';
5+
import '../localization/intl/app_localizations.dart';
6+
import '../override_form_builder_localizations_en.dart';
77

88
void main() {
99
runApp(const MyApp());
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:flutter_form_builder/flutter_form_builder.dart';
3+
import 'package:flutter_localizations/flutter_localizations.dart';
4+
import 'package:form_builder_validators/form_builder_validators.dart';
5+
6+
import '../localization/intl/app_localizations.dart';
7+
8+
final GlobalKey<FormBuilderState> _formKey = GlobalKey<FormBuilderState>();
9+
10+
void main() {
11+
runApp(const _App());
12+
}
13+
14+
class _App extends StatelessWidget {
15+
const _App();
16+
17+
@override
18+
Widget build(BuildContext context) {
19+
return MaterialApp(
20+
title: 'Static vs Dynamic - Example',
21+
theme: ThemeData(primarySwatch: Colors.blue),
22+
home: const _HomePage(),
23+
supportedLocales: AppLocalizations.supportedLocales,
24+
localizationsDelegates: const <LocalizationsDelegate<dynamic>>[
25+
...GlobalMaterialLocalizations.delegates,
26+
// Placed in front of `FormBuilderLocalizations.delegate`
27+
...AppLocalizations.localizationsDelegates,
28+
FormBuilderLocalizations.delegate,
29+
],
30+
);
31+
}
32+
}
33+
34+
class _HomePage extends StatefulWidget {
35+
const _HomePage();
36+
37+
@override
38+
State<_HomePage> createState() => _HomePageState();
39+
}
40+
41+
class _HomePageState extends State<_HomePage> {
42+
int referenceValue = 0;
43+
@override
44+
Widget build(BuildContext context) {
45+
return Scaffold(
46+
appBar: AppBar(title: const Text('Static vs Dynamic - Example')),
47+
body: Padding(
48+
padding: const EdgeInsets.all(8),
49+
child: SingleChildScrollView(
50+
child: Column(
51+
children: <Widget>[
52+
Row(
53+
mainAxisAlignment: MainAxisAlignment.center,
54+
children: <Widget>[
55+
IconButton(
56+
onPressed: () => setState(() {
57+
referenceValue--;
58+
}),
59+
icon: const Icon(Icons.remove)),
60+
Text(referenceValue.toString()),
61+
IconButton(
62+
onPressed: () => setState(() {
63+
referenceValue++;
64+
}),
65+
icon: const Icon(Icons.add)),
66+
],
67+
),
68+
const SizedBox(height: 16),
69+
const Text('With flutter form fields:'),
70+
const SizedBox(height: 8),
71+
TextFormField(
72+
decoration:
73+
const InputDecoration(labelText: 'isEqual (static)'),
74+
autovalidateMode: AutovalidateMode.always,
75+
validator: isRequired(isInt(isEqual(referenceValue))),
76+
),
77+
const SizedBox(height: 8),
78+
TextFormField(
79+
decoration:
80+
const InputDecoration(labelText: 'isEqual (dynamic)'),
81+
autovalidateMode: AutovalidateMode.always,
82+
validator: isRequired(
83+
isInt(isEqual(null, dynValue: () => referenceValue))),
84+
),
85+
const SizedBox(height: 8),
86+
TextFormField(
87+
decoration:
88+
const InputDecoration(labelText: 'isEqual (old API)'),
89+
autovalidateMode: AutovalidateMode.always,
90+
validator:
91+
FormBuilderValidators.equal(referenceValue.toString()),
92+
),
93+
const SizedBox(height: 16),
94+
const Text('With flutter_form_builder:'),
95+
const SizedBox(height: 8),
96+
FormBuilder(
97+
key: _formKey,
98+
autovalidateMode: AutovalidateMode.always,
99+
child: Column(
100+
children: <Widget>[
101+
FormBuilderTextField(
102+
name: 'isEqual (static)',
103+
decoration:
104+
const InputDecoration(labelText: 'isEqual (static)'),
105+
validator: isRequired(isInt(isEqual(referenceValue))),
106+
),
107+
const SizedBox(height: 8),
108+
FormBuilderTextField(
109+
name: 'isEqual (dynamic)',
110+
decoration:
111+
const InputDecoration(labelText: 'isEqual (dynamic)'),
112+
validator: isRequired(
113+
isInt(isEqual(null, dynValue: () => referenceValue))),
114+
),
115+
const SizedBox(height: 8),
116+
FormBuilderTextField(
117+
name: 'isEqual (old API)',
118+
decoration:
119+
const InputDecoration(labelText: 'isEqual (old API)'),
120+
validator: FormBuilderValidators.equal(
121+
referenceValue.toString()),
122+
),
123+
],
124+
),
125+
)
126+
],
127+
),
128+
),
129+
),
130+
);
131+
}
132+
}

example/pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ dependencies:
1212
sdk: flutter
1313
flutter_localizations:
1414
sdk: flutter
15+
flutter_form_builder: ^9.4.1
1516
form_builder_validators:
1617
path: ../
1718

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export 'composition_validators.dart';
2+
export 'conditional_validators.dart';
3+
export 'equality_validators.dart';
4+
export 'override_error_msg.dart';
5+
export 'required_validators.dart';
6+
export 'type_validators.dart';
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import '../../localization/l10n.dart';
2+
import '../constants.dart';
3+
4+
/// This function returns a validator that checks if the user input is equal (using
5+
/// the == operator) to either `value` or `dynValue()`. If the condition is satisfied,
6+
/// the validator returns null, otherwise, it returns
7+
/// `FormBuilderLocalizations.current.equalErrorText(userInput.toString())`, if
8+
/// `equalMsg` is not provided.
9+
///
10+
/// # Parameters
11+
/// Exactly one of the inputs `value` or `dynValue` must be provided. If the value
12+
/// to be compared with is static, provide `value`. Otherwise, provide `dynValue`.
13+
///
14+
/// # Error
15+
/// - Throws [AssertionError] if not exactly one of `value` or `dynValue` was
16+
/// provided.
17+
Validator<T> isEqual<T extends Object?>(
18+
T? value, {
19+
T Function()? dynValue,
20+
String Function(String)? equalMsg,
21+
}) {
22+
assert(
23+
(value != null && dynValue == null) ||
24+
(value == null && dynValue != null),
25+
'Exactly one of "value" or "dynValue" must be provided');
26+
return (T input) {
27+
final T? currentValue = value ?? dynValue?.call();
28+
final String valueString = currentValue!.toString();
29+
return currentValue == input
30+
? null
31+
: equalMsg?.call(valueString) ??
32+
FormBuilderLocalizations.current.equalErrorText(valueString);
33+
};
34+
}

lib/new_api_prototype/generic_type_validators.dart

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,6 @@ Validator<T> containsElement<T extends Object?>(
2222
};
2323
}
2424

25-
Validator<T> isEqual<T extends Object?>(T value,
26-
{String Function(String)? equalMsg}) {
27-
return (input) {
28-
final valueString = value.toString();
29-
return value == input
30-
? null
31-
: equalMsg?.call(valueString) ??
32-
FormBuilderLocalizations.current.equalErrorText(valueString);
33-
};
34-
}
35-
3625
/// Returns a [Validator] function that checks if its `T` input is a `true`
3726
/// boolean or a [String] parsable to a `true` boolean. If the input satisfies
3827
/// this condition, the validator returns `null`. Otherwise, it returns the
Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
export 'collection_validators.dart';
22
export 'constants.dart';
3-
export 'core_validators/composition_validators.dart';
4-
export 'core_validators/override_error_msg.dart';
5-
export 'core_validators/required_validators.dart';
6-
export 'core_validators/type_validators.dart';
3+
export 'core_validators/core_validators.dart';
74
export 'generic_type_validators.dart';
85
export 'numeric_validators.dart';
96
export 'string_validators/string_validators.dart';

0 commit comments

Comments
 (0)