@@ -548,15 +548,29 @@ class FormBuilderValidators {
548
548
static FormFieldValidator <String > fileExtension ({
549
549
required List <String > allowedExtensions,
550
550
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
+ }
560
574
561
575
/// [FormFieldValidator] that restricts the size of an file to be less than or equal to the provided maximum size.
562
576
/// * [maxSize] is the maximum size in bytes.
@@ -565,15 +579,22 @@ class FormBuilderValidators {
565
579
required int maxSize,
566
580
String ? errorText,
567
581
}) =>
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
+ };
577
598
578
599
/// [FormFieldValidator] that applies another validator conditionally.
579
600
/// * [condition] is a function that determines if the validator should be applied.
@@ -582,8 +603,12 @@ class FormBuilderValidators {
582
603
bool Function (T value) condition,
583
604
FormFieldValidator <T > validator,
584
605
) =>
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
+ };
587
612
588
613
/// [FormFieldValidator] that requires the field's value to be within a certain range.
589
614
/// * [minValue] is the minimum value that the field's value should be greater than or equal to.
@@ -596,12 +621,19 @@ class FormBuilderValidators {
596
621
bool inclusive = true ,
597
622
String ? errorText,
598
623
}) {
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
+ };
605
637
}
606
638
607
639
/// [FormFieldValidator] that requires the field's value to be a bool and true.
0 commit comments