Skip to content

Commit 0f222cf

Browse files
committed
Fixed the new validators and updated the tests
1 parent fa53ec4 commit 0f222cf

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

lib/src/form_builder_validators.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,10 +184,10 @@ class FormBuilderValidators {
184184
int valueWordsCount = 0;
185185

186186
if (valueCandidate != null) {
187-
if (valueCandidate.isEmpty) {
187+
if (valueCandidate.trim().isEmpty) {
188188
valueWordsCount = 0;
189189
} else {
190-
valueWordsCount = valueCandidate.split(' ').length;
190+
valueWordsCount = valueCandidate.trim().split(' ').length;
191191
}
192192
}
193193

@@ -206,7 +206,7 @@ class FormBuilderValidators {
206206
}) {
207207
assert(maxCount > 0);
208208
return (valueCandidate) {
209-
int valueWordsCount = valueCandidate?.split(' ').length ?? 0;
209+
int valueWordsCount = valueCandidate?.trim().split(' ').length ?? 0;
210210
return null != valueCandidate && valueWordsCount > maxCount
211211
? errorText ??
212212
FormBuilderLocalizations.current.maxWordsCountErrorText(maxCount)

test/form_builder_validators_test.dart

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,18 @@ void main() {
217217
expect(validator(''), isNull);
218218
expect(validator('the quick brown'), isNull);
219219
expect(validator('The quick brown fox jumps'), isNull);
220+
// 1 White spaces to test the trimming of the string
221+
expect(validator(' '), isNull);
222+
// 5 White spaces to test the trimming of the string
223+
expect(validator(' '), isNull);
224+
// + 1 White space to test the trimming of the string
225+
expect(validator(' The quick brown fox'), isNull);
226+
// + 1 White space to test the trimming of the string
227+
expect(validator('The quick brown fox '), isNull);
228+
// + 1 White space to test the trimming of the string
229+
expect(validator(' The quick brown fox jumps'), isNull);
230+
// + 1 White space to test the trimming of the string
231+
expect(validator('The quick brown fox jumps '), isNull);
220232
// Fail
221233
expect(validator('The quick brown fox jumps over'), isNotNull);
222234
expect(validator('The quick brown fox jumps over the lazy dog'),
@@ -236,6 +248,14 @@ void main() {
236248
expect(validator(null), isNotNull);
237249
expect(validator(''), isNotNull);
238250
expect(validator('The quick brown'), isNotNull);
251+
// 1 White spaces to test the trimming of the string
252+
expect(validator(' '), isNotNull);
253+
// 5 White spaces to test the trimming of the string
254+
expect(validator(' '), isNotNull);
255+
// + 1 White space to test the trimming of the string
256+
expect(validator(' The quick brown fox'), isNotNull);
257+
// + 1 White space to test the trimming of the string
258+
expect(validator('The quick brown fox '), isNotNull);
239259
// Advanced
240260
final validatorAllowEmpty =
241261
FormBuilderValidators.minWordsCount(5, allowEmpty: true);

0 commit comments

Comments
 (0)