From a2b95952dcc63305aeaf31b391e464d39b45befa Mon Sep 17 00:00:00 2001 From: Kate Lovett Date: Thu, 7 Oct 2021 16:48:02 -0500 Subject: [PATCH] Revert "Remove autovalidate deprecations (#90292)" (#91439) --- .../flutter/lib/src/material/date_picker.dart | 36 +--- .../flutter/lib/src/material/dropdown.dart | 16 +- .../lib/src/material/text_form_field.dart | 16 +- .../flutter/lib/src/material/time_picker.dart | 36 +--- packages/flutter/lib/src/widgets/form.dart | 59 ++++++- .../material/dropdown_form_field_test.dart | 23 +++ .../test/material/text_form_field_test.dart | 17 ++ packages/flutter/test/widgets/form_test.dart | 157 +++++++++++++++++- 8 files changed, 288 insertions(+), 72 deletions(-) diff --git a/packages/flutter/lib/src/material/date_picker.dart b/packages/flutter/lib/src/material/date_picker.dart index 19844fc1ed..ddf85c39ea 100644 --- a/packages/flutter/lib/src/material/date_picker.dart +++ b/packages/flutter/lib/src/material/date_picker.dart @@ -355,7 +355,7 @@ class DatePickerDialog extends StatefulWidget { class _DatePickerDialogState extends State with RestorationMixin { late final RestorableDateTime _selectedDate = RestorableDateTime(widget.initialDate); late final _RestorableDatePickerEntryMode _entryMode = _RestorableDatePickerEntryMode(widget.initialEntryMode); - final _RestorableAutovalidateMode _autovalidateMode = _RestorableAutovalidateMode(AutovalidateMode.disabled); + final RestorableBool _autoValidate = RestorableBool(false); @override String? get restorationId => widget.restorationId; @@ -363,7 +363,7 @@ class _DatePickerDialogState extends State with RestorationMix @override void restoreState(RestorationBucket? oldBucket, bool initialRestore) { registerForRestoration(_selectedDate, 'selected_date'); - registerForRestoration(_autovalidateMode, 'autovalidateMode'); + registerForRestoration(_autoValidate, 'autovalidate'); registerForRestoration(_entryMode, 'calendar_entry_mode'); } @@ -374,7 +374,7 @@ class _DatePickerDialogState extends State with RestorationMix if (_entryMode.value == DatePickerEntryMode.input || _entryMode.value == DatePickerEntryMode.inputOnly) { final FormState form = _formKey.currentState!; if (!form.validate()) { - setState(() => _autovalidateMode.value = AutovalidateMode.always); + setState(() => _autoValidate.value = true); return; } form.save(); @@ -390,7 +390,7 @@ class _DatePickerDialogState extends State with RestorationMix setState(() { switch (_entryMode.value) { case DatePickerEntryMode.calendar: - _autovalidateMode.value = AutovalidateMode.disabled; + _autoValidate.value = false; _entryMode.value = DatePickerEntryMode.input; break; case DatePickerEntryMode.input: @@ -492,7 +492,7 @@ class _DatePickerDialogState extends State with RestorationMix Form inputDatePicker() { return Form( key: _formKey, - autovalidateMode: _autovalidateMode.value, + autovalidate: _autoValidate.value, child: Container( padding: const EdgeInsets.symmetric(horizontal: 24), height: orientation == Orientation.portrait ? _inputFormPortraitHeight : _inputFormLandscapeHeight, @@ -642,32 +642,6 @@ class _RestorableDatePickerEntryMode extends RestorableValue value.index; } -// A restorable [AutovalidateMode] value. -// -// This serializes each entry as a unique `int` value. -class _RestorableAutovalidateMode extends RestorableValue { - _RestorableAutovalidateMode( - AutovalidateMode defaultValue, - ) : _defaultValue = defaultValue; - - final AutovalidateMode _defaultValue; - - @override - AutovalidateMode createDefaultValue() => _defaultValue; - - @override - void didUpdateValue(AutovalidateMode? oldValue) { - assert(debugIsSerializableForRestoration(value.index)); - notifyListeners(); - } - - @override - AutovalidateMode fromPrimitives(Object? data) => AutovalidateMode.values[data! as int]; - - @override - Object? toPrimitives() => value.index; -} - /// Re-usable widget that displays the selected date (in large font) and the /// help text above it. /// diff --git a/packages/flutter/lib/src/material/dropdown.dart b/packages/flutter/lib/src/material/dropdown.dart index 0a54d5739e..974ed765a8 100644 --- a/packages/flutter/lib/src/material/dropdown.dart +++ b/packages/flutter/lib/src/material/dropdown.dart @@ -1536,6 +1536,12 @@ class DropdownButtonFormField extends FormField { InputDecoration? decoration, FormFieldSetter? onSaved, FormFieldValidator? validator, + @Deprecated( + 'Use autovalidateMode parameter which provide more specific ' + 'behaviour related to auto validation. ' + 'This feature was deprecated after v1.19.0.', + ) + bool autovalidate = false, AutovalidateMode? autovalidateMode, double? menuMaxHeight, bool? enableFeedback, @@ -1555,13 +1561,21 @@ class DropdownButtonFormField extends FormField { assert(isExpanded != null), assert(itemHeight == null || itemHeight >= kMinInteractiveDimension), assert(autofocus != null), + assert(autovalidate != null), + assert( + autovalidate == false || + autovalidate == true && autovalidateMode == null, + 'autovalidate and autovalidateMode should not be used together.', + ), decoration = decoration ?? InputDecoration(focusColor: focusColor), super( key: key, onSaved: onSaved, initialValue: value, validator: validator, - autovalidateMode: autovalidateMode ?? AutovalidateMode.disabled, + autovalidateMode: autovalidate + ? AutovalidateMode.always + : (autovalidateMode ?? AutovalidateMode.disabled), builder: (FormFieldState field) { final _DropdownButtonFormFieldState state = field as _DropdownButtonFormFieldState; final InputDecoration decorationArg = decoration ?? InputDecoration(focusColor: focusColor); diff --git a/packages/flutter/lib/src/material/text_form_field.dart b/packages/flutter/lib/src/material/text_form_field.dart index 3b1c810814..9d458f321a 100644 --- a/packages/flutter/lib/src/material/text_form_field.dart +++ b/packages/flutter/lib/src/material/text_form_field.dart @@ -116,6 +116,12 @@ class TextFormField extends FormField { SmartDashesType? smartDashesType, SmartQuotesType? smartQuotesType, bool enableSuggestions = true, + @Deprecated( + 'Use autovalidateMode parameter which provide more specific ' + 'behaviour related to auto validation. ' + 'This feature was deprecated after v1.19.0.', + ) + bool autovalidate = false, @Deprecated( 'Use maxLengthEnforcement parameter which provides more specific ' 'behavior related to the maxLength limit. ' @@ -158,6 +164,12 @@ class TextFormField extends FormField { assert(obscureText != null), assert(autocorrect != null), assert(enableSuggestions != null), + assert(autovalidate != null), + assert( + autovalidate == false || + autovalidate == true && autovalidateMode == null, + 'autovalidate and autovalidateMode should not be used together.', + ), assert(maxLengthEnforced != null), assert( maxLengthEnforced || maxLengthEnforcement == null, @@ -186,7 +198,9 @@ class TextFormField extends FormField { onSaved: onSaved, validator: validator, enabled: enabled ?? decoration?.enabled ?? true, - autovalidateMode: autovalidateMode ?? AutovalidateMode.disabled, + autovalidateMode: autovalidate + ? AutovalidateMode.always + : (autovalidateMode ?? AutovalidateMode.disabled), builder: (FormFieldState field) { final _TextFormFieldState state = field as _TextFormFieldState; final InputDecoration effectiveDecoration = (decoration ?? const InputDecoration()) diff --git a/packages/flutter/lib/src/material/time_picker.dart b/packages/flutter/lib/src/material/time_picker.dart index 31377f3168..aed8154106 100644 --- a/packages/flutter/lib/src/material/time_picker.dart +++ b/packages/flutter/lib/src/material/time_picker.dart @@ -1915,32 +1915,6 @@ class _RestorableTimePickerMode extends RestorableValue<_TimePickerMode> { Object? toPrimitives() => value.index; } -// A restorable [AutovalidateMode] value. -// -// This serializes each entry as a unique `int` value. -class _RestorableAutovalidateMode extends RestorableValue { - _RestorableAutovalidateMode( - AutovalidateMode defaultValue, - ) : _defaultValue = defaultValue; - - final AutovalidateMode _defaultValue; - - @override - AutovalidateMode createDefaultValue() => _defaultValue; - - @override - void didUpdateValue(AutovalidateMode? oldValue) { - assert(debugIsSerializableForRestoration(value.index)); - notifyListeners(); - } - - @override - AutovalidateMode fromPrimitives(Object? data) => AutovalidateMode.values[data! as int]; - - @override - Object? toPrimitives() => value.index; -} - // A restorable [_RestorableTimePickerEntryMode] value. // // This serializes each entry as a unique `int` value. @@ -1975,7 +1949,7 @@ class _TimePickerDialogState extends State with RestorationMix late final _RestorableTimePickerEntryMode _entryMode = _RestorableTimePickerEntryMode(widget.initialEntryMode); final _RestorableTimePickerMode _mode = _RestorableTimePickerMode(_TimePickerMode.hour); final _RestorableTimePickerModeN _lastModeAnnounced = _RestorableTimePickerModeN(null); - final _RestorableAutovalidateMode _autovalidateMode = _RestorableAutovalidateMode(AutovalidateMode.disabled); + final RestorableBool _autoValidate = RestorableBool(false); final RestorableBoolN _autofocusHour = RestorableBoolN(null); final RestorableBoolN _autofocusMinute = RestorableBoolN(null); final RestorableBool _announcedInitialTime = RestorableBool(false); @@ -2005,7 +1979,7 @@ class _TimePickerDialogState extends State with RestorationMix registerForRestoration(_entryMode, 'entry_mode'); registerForRestoration(_mode, 'mode'); registerForRestoration(_lastModeAnnounced, 'last_mode_announced'); - registerForRestoration(_autovalidateMode, 'autovalidateMode'); + registerForRestoration(_autoValidate, 'autovalidate'); registerForRestoration(_autofocusHour, 'autofocus_hour'); registerForRestoration(_autofocusMinute, 'autofocus_minute'); registerForRestoration(_announcedInitialTime, 'announced_initial_time'); @@ -2048,7 +2022,7 @@ class _TimePickerDialogState extends State with RestorationMix setState(() { switch (_entryMode.value) { case TimePickerEntryMode.dial: - _autovalidateMode.value = AutovalidateMode.disabled; + _autoValidate.value = false; _entryMode.value = TimePickerEntryMode.input; break; case TimePickerEntryMode.input: @@ -2122,7 +2096,7 @@ class _TimePickerDialogState extends State with RestorationMix if (_entryMode.value == TimePickerEntryMode.input) { final FormState form = _formKey.currentState!; if (!form.validate()) { - setState(() { _autovalidateMode.value = AutovalidateMode.always; }); + setState(() { _autoValidate.value = true; }); return; } form.save(); @@ -2283,7 +2257,7 @@ class _TimePickerDialogState extends State with RestorationMix case TimePickerEntryMode.input: picker = Form( key: _formKey, - autovalidateMode: _autovalidateMode.value, + autovalidate: _autoValidate.value, child: SingleChildScrollView( restorationId: 'time_picker_scroll_view', child: Column( diff --git a/packages/flutter/lib/src/widgets/form.dart b/packages/flutter/lib/src/widgets/form.dart index dd4d87a97c..27e2b54da6 100644 --- a/packages/flutter/lib/src/widgets/form.dart +++ b/packages/flutter/lib/src/widgets/form.dart @@ -40,11 +40,24 @@ class Form extends StatefulWidget { const Form({ Key? key, required this.child, + @Deprecated( + 'Use autovalidateMode parameter which provides more specific ' + 'behavior related to auto validation. ' + 'This feature was deprecated after v1.19.0.', + ) + this.autovalidate = false, this.onWillPop, this.onChanged, AutovalidateMode? autovalidateMode, }) : assert(child != null), - autovalidateMode = autovalidateMode ?? AutovalidateMode.disabled, + assert(autovalidate != null), + assert( + autovalidate == false || + autovalidate == true && autovalidateMode == null, + 'autovalidate and autovalidateMode should not be used together.', + ), + autovalidateMode = autovalidateMode ?? + (autovalidate ? AutovalidateMode.always : AutovalidateMode.disabled), super(key: key); /// Returns the closest [FormState] which encloses the given context. @@ -91,6 +104,15 @@ class Form extends StatefulWidget { /// {@macro flutter.widgets.FormField.autovalidateMode} final AutovalidateMode autovalidateMode; + /// Used to enable/disable form fields auto validation and update their error + /// text. + @Deprecated( + 'Use autovalidateMode parameter which provides more specific ' + 'behavior related to auto validation. ' + 'This feature was deprecated after v1.19.0.', + ) + final bool autovalidate; + @override FormState createState() => FormState(); } @@ -265,11 +287,23 @@ class FormField extends StatefulWidget { this.onSaved, this.validator, this.initialValue, + @Deprecated( + 'Use autovalidateMode parameter which provides more specific ' + 'behavior related to auto validation. ' + 'This feature was deprecated after v1.19.0.', + ) + this.autovalidate = false, this.enabled = true, AutovalidateMode? autovalidateMode, this.restorationId, }) : assert(builder != null), - autovalidateMode = autovalidateMode ?? AutovalidateMode.disabled, + assert( + autovalidate == false || + autovalidate == true && autovalidateMode == null, + 'autovalidate and autovalidateMode should not be used together.', + ), + autovalidateMode = autovalidateMode ?? + (autovalidate ? AutovalidateMode.always : AutovalidateMode.disabled), super(key: key); /// An optional method to call with the final value when the form is saved via @@ -310,15 +344,26 @@ class FormField extends StatefulWidget { /// error text. /// /// {@template flutter.widgets.FormField.autovalidateMode} - /// If [AutovalidateMode.onUserInteraction], this FormField will only - /// auto-validate after its content changes. If [AutovalidateMode.always], it - /// will auto-validate even without user interaction. If - /// [AutovalidateMode.disabled], auto-validation will be disabled. + /// If [AutovalidateMode.onUserInteraction] this form field will only + /// auto-validate after its content changes, if [AutovalidateMode.always] it + /// will auto validate even without user interaction and + /// if [AutovalidateMode.disabled] the auto validation will be disabled. /// - /// Defaults to [AutovalidateMode.disabled], cannot be null. + /// Defaults to [AutovalidateMode.disabled] if `autovalidate` is false which + /// means no auto validation will occur. If `autovalidate` is true then this + /// is set to [AutovalidateMode.always] for backward compatibility. /// {@endtemplate} final AutovalidateMode autovalidateMode; + /// Used to enable/disable auto validation and update their error + /// text. + @Deprecated( + 'Use autovalidateMode parameter which provides more specific ' + 'behavior related to auto validation. ' + 'This feature was deprecated after v1.19.0.', + ) + final bool autovalidate; + /// Restoration ID to save and restore the state of the form field. /// /// Setting the restoration ID to a non-null value results in whether or not diff --git a/packages/flutter/test/material/dropdown_form_field_test.dart b/packages/flutter/test/material/dropdown_form_field_test.dart index 9ac2d7c7a2..e1ad316895 100644 --- a/packages/flutter/test/material/dropdown_form_field_test.dart +++ b/packages/flutter/test/material/dropdown_form_field_test.dart @@ -1086,6 +1086,29 @@ void main() { expect(_validateCalled, 1); }); + testWidgets('autovalidateMode and autovalidate should not be used at the same time', (WidgetTester tester) async { + Widget builder() { + return MaterialApp( + home: Material( + child: Center( + child: DropdownButtonFormField( + autovalidate: true, + autovalidateMode: AutovalidateMode.always, + items: menuItems.map((String value) { + return DropdownMenuItem( + value: value, + child: Text(value), + ); + }).toList(), + onChanged: onChanged, + ), + ), + ), + ); + } + expect(() => builder(), throwsAssertionError); + }); + testWidgets('DropdownButtonFormField - Custom button alignment', (WidgetTester tester) async { await tester.pumpWidget(buildFormFrame( buttonAlignment: AlignmentDirectional.center, diff --git a/packages/flutter/test/material/text_form_field_test.dart b/packages/flutter/test/material/text_form_field_test.dart index e45f5c2e69..ab7edb6aaa 100644 --- a/packages/flutter/test/material/text_form_field_test.dart +++ b/packages/flutter/test/material/text_form_field_test.dart @@ -653,6 +653,23 @@ void main() { expect(_validateCalled, 1); }); + testWidgets('autovalidateMode and autovalidate should not be used at the same time', (WidgetTester tester) async { + expect(() async { + await tester.pumpWidget( + MaterialApp( + home: Material( + child: Scaffold( + body: TextFormField( + autovalidate: true, + autovalidateMode: AutovalidateMode.onUserInteraction, + ), + ), + ), + ), + ); + }, throwsAssertionError); + }); + testWidgets('textSelectionControls is passed to super', (WidgetTester tester) async { await tester.pumpWidget( MaterialApp( diff --git a/packages/flutter/test/widgets/form_test.dart b/packages/flutter/test/widgets/form_test.dart index 391790232e..eb47fa9d18 100644 --- a/packages/flutter/test/widgets/form_test.dart +++ b/packages/flutter/test/widgets/form_test.dart @@ -684,7 +684,7 @@ void main() { ); } - // Makes sure the Form widget won't auto-validate the form fields + // Makes sure the Form widget won't autovalidate the form fields // after rebuilds if there is not user interaction. await tester.pumpWidget(builder()); await tester.pumpWidget(builder()); @@ -733,6 +733,38 @@ void main() { expect(find.text(errorText('')!), findsOneWidget); }); + testWidgets('autovalidate parameter is still used if true', (WidgetTester tester) async { + late FormFieldState formFieldState; + String? errorText(String? value) => '$value/error'; + + Widget builder() { + return MaterialApp( + home: MediaQuery( + data: const MediaQueryData(devicePixelRatio: 1.0), + child: Directionality( + textDirection: TextDirection.ltr, + child: Center( + child: Material( + child: FormField( + initialValue: 'foo', + autovalidate: true, + builder: (FormFieldState state) { + formFieldState = state; + return Container(); + }, + validator: errorText, + ), + ), + ), + ), + ), + ); + } + + await tester.pumpWidget(builder()); + expect(formFieldState.hasError, isTrue); + }); + testWidgets('Form.reset() resets form fields, and auto validation will only happen on the next user interaction if autovalidateMode is onUserInteraction', (WidgetTester tester) async { final GlobalKey formState = GlobalKey(); String? errorText(String? value) => '$value/error'; @@ -777,6 +809,46 @@ void main() { expect(find.text(errorText('bar')!), findsNothing); }); + testWidgets('Form.autovalidateMode and Form.autovalidate should not be used at the same time', (WidgetTester tester) async { + Widget builder() { + return MaterialApp( + home: MediaQuery( + data: const MediaQueryData(devicePixelRatio: 1.0), + child: Directionality( + textDirection: TextDirection.ltr, + child: Form( + autovalidate: true, + autovalidateMode: AutovalidateMode.disabled, + child: Container(), + ), + ), + ), + ); + } + expect(() => builder(), throwsAssertionError); + }); + + testWidgets('FormField.autovalidateMode and FormField.autovalidate should not be used at the same time', (WidgetTester tester) async { + Widget builder() { + return MaterialApp( + home: MediaQuery( + data: const MediaQueryData(devicePixelRatio: 1.0), + child: Directionality( + textDirection: TextDirection.ltr, + child: FormField( + autovalidate: true, + autovalidateMode: AutovalidateMode.disabled, + builder: (_) { + return Container(); + }, + ), + ), + ), + ); + } + expect(() => builder(), throwsAssertionError); + }); + // Regression test for https://github.com/flutter/flutter/issues/63753. testWidgets('Validate form should return correct validation if the value is composing', (WidgetTester tester) async { final GlobalKey formKey = GlobalKey(); @@ -814,4 +886,87 @@ void main() { expect(fieldValue, '123456'); expect(formKey.currentState!.validate(), isFalse); }); + + testWidgets('FormField.autovalidate parameter is passed into class the property', (WidgetTester tester) async { + String? errorText(String? value) => '$value/error'; + const ObjectKey widgetKey = ObjectKey('key'); + + Widget builder({required bool autovalidate}) { + return MaterialApp( + home: MediaQuery( + data: const MediaQueryData(devicePixelRatio: 1.0), + child: Directionality( + textDirection: TextDirection.ltr, + child: Center( + child: Material( + child: FormField( + key: widgetKey, + initialValue: 'foo', + autovalidate: autovalidate, + builder: (FormFieldState state) { + return Container(); + }, + validator: errorText, + ), + ), + ), + ), + ), + ); + } + + // When autovalidate is true + await tester.pumpWidget(builder(autovalidate: true)); + + final Finder formFieldFinder = find.byKey(widgetKey); + FormField formField = tester.widget(formFieldFinder); + expect(formField.autovalidate, isTrue); + expect(formField.autovalidateMode, equals(AutovalidateMode.always)); + + // When autovalidate is false + await tester.pumpWidget(builder(autovalidate: false)); + + formField = tester.widget(formFieldFinder); + expect(formField.autovalidate, isFalse); + expect(formField.autovalidateMode, equals(AutovalidateMode.disabled)); + }); + + testWidgets('Form.autovalidate parameter is passed into class the property', (WidgetTester tester) async { + const ObjectKey widgetKey = ObjectKey('key'); + + Widget builder({required bool autovalidate}) { + return MaterialApp( + home: MediaQuery( + data: const MediaQueryData(devicePixelRatio: 1.0), + child: Directionality( + textDirection: TextDirection.ltr, + child: Center( + child: Material( + child: Form( + key: widgetKey, + autovalidate: autovalidate, + child: Container(), + ), + ), + ), + ), + ), + ); + } + + // When autovalidate is true + await tester.pumpWidget(builder(autovalidate: true)); + + final Finder formFinder = find.byKey(widgetKey); + Form formWidget = tester.widget(formFinder); + expect(formWidget.autovalidate, isTrue); + expect(formWidget.autovalidateMode, equals(AutovalidateMode.always)); + + // When autovalidate is false + await tester.pumpWidget(builder(autovalidate: false)); + + formWidget = tester.widget(formFinder); + expect(formWidget.autovalidate, isFalse); + expect(formWidget.autovalidateMode, equals(AutovalidateMode.disabled)); + }); }