Skip to content

Commit e19d17d

Browse files
committed
Fix date range
1 parent a3efb01 commit e19d17d

File tree

1 file changed

+16
-36
lines changed

1 file changed

+16
-36
lines changed

lib/src/form_builder_validators.dart

Lines changed: 16 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -357,43 +357,23 @@ class FormBuilderValidators {
357357

358358
/// [FormFieldValidator] that requires the field's value to be a date within a certain range.
359359
static FormFieldValidator<String> dateRange({
360-
required String minDate,
361-
required String maxDate,
360+
required DateTime minDate,
361+
required DateTime maxDate,
362362
String? errorText,
363-
}) {
364-
return compose<String>(
365-
[
366-
minDate.isNotEmpty
367-
? dateString(errorText: errorText)
368-
: (valueCandidate) => null,
369-
maxDate.isNotEmpty
370-
? dateString(errorText: errorText)
371-
: (valueCandidate) => null,
372-
(valueCandidate) {
373-
if (valueCandidate == null || valueCandidate.isEmpty) {
374-
return null;
375-
}
376-
377-
final minDateTime = DateTime.tryParse(minDate);
378-
final maxDateTime = DateTime.tryParse(maxDate);
379-
final valueDateTime = DateTime.tryParse(valueCandidate);
380-
381-
if (minDateTime != null &&
382-
valueDateTime!.isBefore(minDateTime) &&
383-
maxDateTime != null &&
384-
valueDateTime.isAfter(maxDateTime)) {
385-
return errorText ??
386-
FormBuilderLocalizations.current.dateRangeErrorText(
387-
minDate,
388-
maxDate,
389-
);
390-
}
391-
392-
return null;
393-
},
394-
],
395-
);
396-
}
363+
}) =>
364+
(String? valueCandidate) {
365+
if (valueCandidate == null || !isDate(valueCandidate)) {
366+
return errorText ??
367+
FormBuilderLocalizations.current.dateStringErrorText;
368+
}
369+
370+
final DateTime date = DateTime.parse(valueCandidate);
371+
return date.isBefore(minDate) || date.isAfter(maxDate)
372+
? errorText ??
373+
FormBuilderLocalizations.current
374+
.dateRangeErrorText(minDate.toString(), maxDate.toString())
375+
: null;
376+
};
397377

398378
/// [FormFieldValidator] that requires the field's value to be a valid phone number.
399379
static FormFieldValidator<String> phoneNumber({

0 commit comments

Comments
 (0)