Skip to content

Commit 0326298

Browse files
committed
Add singleline
1 parent 6653240 commit 0326298

File tree

6 files changed

+35
-2
lines changed

6 files changed

+35
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
* Add aggregate
4343
* Add unique
4444
* Add ISBN
45+
* Add singleLine
4546

4647
## 10.0.1
4748
* Fix regression (include l10n files)

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ Available built-in type validators include:
8888
- `FormBuilderValidators.between()` - requires the field's to be between two numbers.
8989
- `FormBuilderValidators.containsElement()` - requires the field's to be in the provided list.
9090
- `FormBuilderValidators.unique()` - requires the field's to be unique in the provided list.
91+
- `FormBuilderValidators.singleLine()` - requires the field's string to be a single line of text.
9192

9293
Available built-in use-case validators include:
9394

lib/l10n/intl_en.arb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,6 @@
5353
"ibanErrorText": "Value must be a valid IBAN.",
5454
"uniqueErrorText": "Value must be unique.",
5555
"bicErrorText": "Value must be a valid BIC.",
56-
"isbnErrorText": "Value must be a valid ISBN."
56+
"isbnErrorText": "Value must be a valid ISBN.",
57+
"singleLineErrorText": "Value must be a single line."
5758
}

lib/src/form_builder_validators.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,4 +1050,14 @@ class FormBuilderValidators {
10501050
valueCandidate?.isEmpty != false || !isISBN(valueCandidate!)
10511051
? errorText ?? FormBuilderLocalizations.current.isbnErrorText
10521052
: null;
1053+
1054+
/// [FormFieldValidator] that requires the field's value to be a single line.
1055+
/// * [errorText] is the error message to display when the value is not a single line
1056+
static FormFieldValidator<String> singleLine({
1057+
String? errorText,
1058+
}) =>
1059+
(valueCandidate) => valueCandidate?.isEmpty != false ||
1060+
!isSingleLine(valueCandidate!)
1061+
? errorText ?? FormBuilderLocalizations.current.singleLineErrorText
1062+
: null;
10531063
}

lib/src/utils/validators.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,3 +508,7 @@ bool isISBN(String isbn) {
508508
return false;
509509
}
510510
}
511+
512+
bool isSingleLine(String value) {
513+
return !value.contains('\n') && !value.contains('\r');
514+
}

test/form_builder_validators_test.dart

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1186,10 +1186,26 @@ void main() {
11861186
expect(validator('0-306-40615-2'), isNull); // A valid ISBN-10
11871187
// Fail
11881188
expect(validator(null), isNotNull);
1189-
expect(validator(''), isNotNull); // Empty string
1189+
expect(validator(''), isNotNull);
11901190
expect(validator('INVALIDISBN'), isNotNull); // Invalid ISBN
11911191
expect(validator('978-3-16-148410-00'), isNotNull); // Too long
11921192
expect(validator('3-16-148410-00'), isNotNull); // Too short
11931193
}),
11941194
);
1195+
1196+
testWidgets(
1197+
'FormBuilderValidators.singleLine',
1198+
(WidgetTester tester) => testValidations(tester, (context) {
1199+
final validator = FormBuilderValidators.singleLine();
1200+
// Pass
1201+
expect(validator('The quick brown fox jumps'), isNull);
1202+
// Fail
1203+
expect(validator(null), isNotNull);
1204+
expect(validator(''), isNotNull);
1205+
expect(validator('The quick brown fox\njumps'), isNotNull);
1206+
expect(validator('The quick brown fox\rjumps'), isNotNull);
1207+
expect(validator('The quick brown fox\r\njumps'), isNotNull);
1208+
expect(validator('The quick brown fox\n\rjumps'), isNotNull);
1209+
}),
1210+
);
11951211
}

0 commit comments

Comments
 (0)