Skip to content

Commit 55eee98

Browse files
authored
Add FormBuilderValidators.notEqual validator (#625)
* Add FormBuilderValidators.notEqual validator For ensuring a value is not-equal to a given value. Slightly more elegant & eloquent than manually implementing a function for this. * Update Chips Input dependency version explicitly * Update Chips Input dependency version explicitly
1 parent 7134597 commit 55eee98

File tree

4 files changed

+31
-2
lines changed

4 files changed

+31
-2
lines changed

lib/localization/form_builder_localizations.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ class FormBuilderLocalizations {
4545
desc: 'Error Text for equal validator',
4646
);
4747

48+
String notEqualErrorText<T>(T value) => Intl.message(
49+
'This field value must not be equal to $value.',
50+
name: 'notEqualErrorText',
51+
args: [value],
52+
desc: 'Error Text for not-equal validator',
53+
);
54+
4855
String minErrorText(num min) => Intl.message(
4956
'Value must be greater than or equal to $min.',
5057
name: 'minErrorText',

lib/src/form_builder_validators.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,17 @@ class FormBuilderValidators {
4949
FormBuilderLocalizations.of(context).equalErrorText(value)
5050
: null;
5151

52+
/// [FormFieldValidator] that requires the field's value be not equal to
53+
/// the provided value.
54+
static FormFieldValidator<T> notEqual<T>(
55+
BuildContext context,
56+
T value, {
57+
String errorText,
58+
}) =>
59+
(valueCandidate) => valueCandidate == value
60+
? errorText ?? FormBuilderLocalizations.of(context).notEqualErrorText(value)
61+
: null;
62+
5263
/// [FormFieldValidator] that requires the field's value to be greater than
5364
/// (or equal) to the provided number.
5465
static FormFieldValidator<T> min<T>(

pubspec.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: flutter_form_builder
22
description: This package helps in creation of forms in Flutter by removing the boilerplate code, reusing validation, react to changes, and collect final user input.
3-
version: 4.0.2
3+
version: 4.0.2+1
44
homepage: https://github.com/danvick/flutter_form_builder
55

66
environment:
@@ -20,7 +20,7 @@ dependencies:
2020
dropdown_search: ^0.4.8
2121
file_picker: ^2.1.0
2222
flutter_colorpicker: ^0.3.4
23-
flutter_chips_input: ^1.9.4
23+
flutter_chips_input: ^1.9.5
2424
flutter_datetime_picker: ^1.4.0
2525
flutter_touch_spin: ^1.0.1
2626
flutter_typeahead: ^1.9.1

test/form_builder_validators_test.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,17 @@ void main() {
9191
expect(validator(false), isNotNull);
9292
}));
9393

94+
testWidgets(
95+
'FormBuilderValidators.notEqual',
96+
(WidgetTester tester) => testValidations(tester, (context) {
97+
final validator = FormBuilderValidators.notEqual(context, true);
98+
// Pass
99+
expect(validator(false), isNull);
100+
expect(validator(null), isNull);
101+
// Fail
102+
expect(validator(true), isNotNull);
103+
}));
104+
94105
testWidgets(
95106
'FormBuilderValidators.maxLength',
96107
(WidgetTester tester) => testValidations(tester, (context) {

0 commit comments

Comments
 (0)