Skip to content

Commit 72033b1

Browse files
committed
Tests
1 parent 73e990e commit 72033b1

File tree

2 files changed

+126
-58
lines changed

2 files changed

+126
-58
lines changed

lib/src/form_builder_validators.dart

Lines changed: 58 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -548,15 +548,29 @@ class FormBuilderValidators {
548548
static FormFieldValidator<String> fileExtension({
549549
required List<String> allowedExtensions,
550550
String? errorText,
551-
}) =>
552-
(valueCandidate) => valueCandidate == null
553-
? null
554-
: !allowedExtensions
555-
.contains(fileExtensionFromPath(valueCandidate).toLowerCase())
556-
? errorText ??
557-
FormBuilderLocalizations.current
558-
.fileExtensionErrorText(allowedExtensions.join(', '))
559-
: null;
551+
}) {
552+
final allowedExtensionsLowerCase =
553+
allowedExtensions.map((e) => e.toLowerCase()).toList();
554+
555+
return (valueCandidate) {
556+
if (valueCandidate == null || valueCandidate.isEmpty) {
557+
return errorText ??
558+
FormBuilderLocalizations.current.fileExtensionErrorText(
559+
allowedExtensions.join(', '),
560+
);
561+
}
562+
563+
final extension = fileExtensionFromPath(valueCandidate).toLowerCase();
564+
if (!allowedExtensionsLowerCase.contains(extension)) {
565+
return errorText ??
566+
FormBuilderLocalizations.current.fileExtensionErrorText(
567+
allowedExtensions.join(', '),
568+
);
569+
}
570+
571+
return null;
572+
};
573+
}
560574

561575
/// [FormFieldValidator] that restricts the size of an file to be less than or equal to the provided maximum size.
562576
/// * [maxSize] is the maximum size in bytes.
@@ -565,15 +579,22 @@ class FormBuilderValidators {
565579
required int maxSize,
566580
String? errorText,
567581
}) =>
568-
(valueCandidate) => valueCandidate == null
569-
? null
570-
: int.parse(valueCandidate) > maxSize
571-
? errorText ??
572-
FormBuilderLocalizations.current.fileSizeErrorText(
573-
formatBytes(int.parse(valueCandidate)),
574-
formatBytes(maxSize),
575-
)
576-
: null;
582+
(valueCandidate) {
583+
if (valueCandidate == null || valueCandidate.isEmpty) {
584+
return errorText ??
585+
FormBuilderLocalizations.current
586+
.fileSizeErrorText(formatBytes(0), formatBytes(maxSize));
587+
}
588+
589+
final size = int.tryParse(valueCandidate);
590+
if (size == null || size > maxSize) {
591+
return errorText ??
592+
FormBuilderLocalizations.current.fileSizeErrorText(
593+
formatBytes(size ?? 0), formatBytes(maxSize));
594+
}
595+
596+
return null;
597+
};
577598

578599
/// [FormFieldValidator] that applies another validator conditionally.
579600
/// * [condition] is a function that determines if the validator should be applied.
@@ -582,8 +603,12 @@ class FormBuilderValidators {
582603
bool Function(T value) condition,
583604
FormFieldValidator<T> validator,
584605
) =>
585-
(T? valueCandidate) =>
586-
condition(valueCandidate as T) ? validator(valueCandidate) : null;
606+
(valueCandidate) {
607+
if (valueCandidate != null && condition(valueCandidate)) {
608+
return validator(valueCandidate);
609+
}
610+
return null;
611+
};
587612

588613
/// [FormFieldValidator] that requires the field's value to be within a certain range.
589614
/// * [minValue] is the minimum value that the field's value should be greater than or equal to.
@@ -596,12 +621,19 @@ class FormBuilderValidators {
596621
bool inclusive = true,
597622
String? errorText,
598623
}) {
599-
return compose<T>(
600-
[
601-
min(minValue, inclusive: inclusive, errorText: errorText),
602-
max(maxValue, inclusive: inclusive, errorText: errorText),
603-
],
604-
);
624+
return (T? valueCandidate) {
625+
assert(valueCandidate is num || valueCandidate is String);
626+
final number = valueCandidate is num
627+
? valueCandidate
628+
: num.tryParse(valueCandidate.toString());
629+
630+
final minResult =
631+
min(minValue, inclusive: inclusive, errorText: errorText)(number);
632+
final maxResult =
633+
max(maxValue, inclusive: inclusive, errorText: errorText)(number);
634+
635+
return errorText ?? minResult ?? maxResult;
636+
};
605637
}
606638

607639
/// [FormFieldValidator] that requires the field's value to be a bool and true.

0 commit comments

Comments
 (0)