From 10f6284426bc653da3640088c0a769fab7986cc9 Mon Sep 17 00:00:00 2001 From: Pedro Massango Date: Mon, 28 Sep 2020 17:27:16 +0100 Subject: [PATCH] Add back the autovalidate class property (#66267) * Add back autovalidate property * Add autovalidate property back (include tests) * Fix typos * One more test :) --- packages/flutter/lib/src/widgets/form.dart | 40 +++++++--- packages/flutter/test/widgets/form_test.dart | 83 ++++++++++++++++++++ 2 files changed, 111 insertions(+), 12 deletions(-) diff --git a/packages/flutter/lib/src/widgets/form.dart b/packages/flutter/lib/src/widgets/form.dart index a67a6e5776..097f45ccc6 100644 --- a/packages/flutter/lib/src/widgets/form.dart +++ b/packages/flutter/lib/src/widgets/form.dart @@ -77,11 +77,11 @@ class Form extends StatefulWidget { Key? key, required this.child, @Deprecated( - 'Use autoValidateMode parameter which provide more specific ' - 'behaviour related to auto validation. ' + 'Use autoValidateMode parameter which provides more specific ' + 'behavior related to auto validation. ' 'This feature was deprecated after v1.19.0.' ) - bool autovalidate = false, + this.autovalidate = false, this.onWillPop, this.onChanged, AutovalidateMode? autovalidateMode, @@ -92,9 +92,8 @@ class Form extends StatefulWidget { autovalidate == true && autovalidateMode == null, 'autovalidate and autovalidateMode should not be used together.' ), - autovalidateMode = autovalidate - ? AutovalidateMode.always - : (autovalidateMode ?? AutovalidateMode.disabled), + autovalidateMode = autovalidateMode ?? + (autovalidate ? AutovalidateMode.always : AutovalidateMode.disabled), super(key: key); /// Returns the closest [FormState] which encloses the given context. @@ -141,6 +140,15 @@ class Form extends StatefulWidget { /// {@macro flutter.widgets.form.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(); } @@ -318,11 +326,11 @@ class FormField extends StatefulWidget { this.validator, this.initialValue, @Deprecated( - 'Use autoValidateMode parameter which provide more specific ' - 'behaviour related to auto validation. ' + 'Use autoValidateMode parameter which provides more specific ' + 'behavior related to auto validation. ' 'This feature was deprecated after v1.19.0.' ) - bool autovalidate = false, + this.autovalidate = false, this.enabled = true, AutovalidateMode? autovalidateMode, }) : assert(builder != null), @@ -331,9 +339,8 @@ class FormField extends StatefulWidget { autovalidate == true && autovalidateMode == null, 'autovalidate and autovalidateMode should not be used together.' ), - autovalidateMode = autovalidate - ? AutovalidateMode.always - : (autovalidateMode ?? AutovalidateMode.disabled), + 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 @@ -385,6 +392,15 @@ class FormField extends StatefulWidget { /// {@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; + @override FormFieldState createState() => FormFieldState(); } diff --git a/packages/flutter/test/widgets/form_test.dart b/packages/flutter/test/widgets/form_test.dart index 7010b799e6..657652dc77 100644 --- a/packages/flutter/test/widgets/form_test.dart +++ b/packages/flutter/test/widgets/form_test.dart @@ -886,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)); + }); }