datetime_picker_formfield
datetime_picker_formfield copied to clipboard
Validation passes in null if controller text is assigned instead
If the controller text is set to a value instead of the user choosing the date and time from the pickers the validation function will receive null.
- If you have a form with the DateTime widget:
return Form(
key: formKey,
child: DateTimeField(
validator: validator,
controller: controller,
),
);
- and you validate after assigning the controller a text:
timeController.text = startDate.toString();
formKey.currentState.validate()
- With this validation:
String validateDateTime(DateTime dateTime) {
// fails this check
if (dateTime == null) return 'This field is required';
return null;
}
- The validation will fail
timeController.text = startDate.toString();
If that doesn't match theDateFormat
passed to the widget, the value during validate()
will be null.
Alright thanks, ill check it out
I'm having the same problem.
I'm using the same DateFormat
to format the controller text and as format for the DateTimeField
.
final format = DateFormat("dd-MM-yyyy");
final dateController = TextEditingController();
dateController.text = format.format(DateTime.parse("2019-10-31"));
DateTimeField(
format: format,
controller: dateController,
readOnly: true,
onShowPicker: (context, currentValue) {
return showDatePicker(
context: context,
firstDate: DateTime(1900),
initialDate: currentValue ?? DateTime.now(),
lastDate: DateTime(2100));
},
validator: (date) => date == null ? 'Invalid date' : null,
),
The validator is receiving null
and fails.
@jifalops I added a sample DateTime value to the example app in BasicDatePicker like so:
class BasicDateField extends StatelessWidget {
final format = DateFormat("yyyy-MM-dd");
@override
Widget build(BuildContext context) {
return Column(children: <Widget>[
Text('Basic date field (${format.pattern})'),
DateTimeField(
format: format,
controller: TextEditingController(text: '2019-09-30'),
validator: (dateTime) {
print(dateTime);
},
onShowPicker: (context, currentValue) {
return showDatePicker(
context: context,
firstDate: DateTime(1900),
initialDate: currentValue ?? DateTime.now(),
lastDate: DateTime(2100));
},
),
]);
}
}
- Validator will still recieve null if DateTime is assigned programatically.
- Validator will only work if DateTime is picked from the UI.
same issue.
It helped for me:
DateTimeField(
controller: _dateEditController,
validator: (value) => _dateEditController.text.isEmpty
? "'date' cann't be empty"
: null,
format: _format,
onShowPicker: (context, currentValue) {
return showDatePicker(
context: context,
firstDate: DateTime(2010),
initialDate: currentValue ?? DateTime.now(),
lastDate: DateTime(2100));
},
),
Same problem here. The validator works just for the inputFIeld inside the dialog picker when you click the pen for type custom date. For the text field shown by DateTimeField itself, the validations doesn't work.
This worked for me, for the same problem:
validator: (s) => validator( controller.value ),
onChanged: (dt) {
controller.value = dt;