datetime_picker_formfield icon indicating copy to clipboard operation
datetime_picker_formfield copied to clipboard

Validation passes in null if controller text is assigned instead

Open VictorUvarov opened this issue 4 years ago • 8 comments

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

VictorUvarov avatar Aug 26 '19 17:08 VictorUvarov

timeController.text = startDate.toString();

If that doesn't match theDateFormat passed to the widget, the value during validate() will be null.

jifalops avatar Sep 04 '19 14:09 jifalops

Alright thanks, ill check it out

VictorUvarov avatar Sep 04 '19 18:09 VictorUvarov

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.

Agnilor avatar Sep 05 '19 10:09 Agnilor

@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.

VictorUvarov avatar Sep 30 '19 17:09 VictorUvarov

same issue.

tegarkurniawan avatar Jan 06 '20 06:01 tegarkurniawan

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));
            },
          ),

lisenkoo-hyuna avatar Feb 11 '20 13:02 lisenkoo-hyuna

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.

JRamos29 avatar Jun 07 '20 15:06 JRamos29

This worked for me, for the same problem:

validator: (s) => validator( controller.value ),
onChanged: (dt) {
              controller.value = dt;

fpv999 avatar Nov 18 '20 21:11 fpv999