Unverified Commit 10f62844 authored by Pedro Massango's avatar Pedro Massango Committed by GitHub

Add back the autovalidate class property (#66267)

* Add back autovalidate property

* Add autovalidate property back (include tests)

* Fix typos

* One more test :)
parent d95bff1e
...@@ -77,11 +77,11 @@ class Form extends StatefulWidget { ...@@ -77,11 +77,11 @@ class Form extends StatefulWidget {
Key? key, Key? key,
required this.child, required this.child,
@Deprecated( @Deprecated(
'Use autoValidateMode parameter which provide more specific ' 'Use autoValidateMode parameter which provides more specific '
'behaviour related to auto validation. ' 'behavior related to auto validation. '
'This feature was deprecated after v1.19.0.' 'This feature was deprecated after v1.19.0.'
) )
bool autovalidate = false, this.autovalidate = false,
this.onWillPop, this.onWillPop,
this.onChanged, this.onChanged,
AutovalidateMode? autovalidateMode, AutovalidateMode? autovalidateMode,
...@@ -92,9 +92,8 @@ class Form extends StatefulWidget { ...@@ -92,9 +92,8 @@ class Form extends StatefulWidget {
autovalidate == true && autovalidateMode == null, autovalidate == true && autovalidateMode == null,
'autovalidate and autovalidateMode should not be used together.' 'autovalidate and autovalidateMode should not be used together.'
), ),
autovalidateMode = autovalidate autovalidateMode = autovalidateMode ??
? AutovalidateMode.always (autovalidate ? AutovalidateMode.always : AutovalidateMode.disabled),
: (autovalidateMode ?? AutovalidateMode.disabled),
super(key: key); super(key: key);
/// Returns the closest [FormState] which encloses the given context. /// Returns the closest [FormState] which encloses the given context.
...@@ -141,6 +140,15 @@ class Form extends StatefulWidget { ...@@ -141,6 +140,15 @@ class Form extends StatefulWidget {
/// {@macro flutter.widgets.form.autovalidateMode} /// {@macro flutter.widgets.form.autovalidateMode}
final AutovalidateMode 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 @override
FormState createState() => FormState(); FormState createState() => FormState();
} }
...@@ -318,11 +326,11 @@ class FormField<T> extends StatefulWidget { ...@@ -318,11 +326,11 @@ class FormField<T> extends StatefulWidget {
this.validator, this.validator,
this.initialValue, this.initialValue,
@Deprecated( @Deprecated(
'Use autoValidateMode parameter which provide more specific ' 'Use autoValidateMode parameter which provides more specific '
'behaviour related to auto validation. ' 'behavior related to auto validation. '
'This feature was deprecated after v1.19.0.' 'This feature was deprecated after v1.19.0.'
) )
bool autovalidate = false, this.autovalidate = false,
this.enabled = true, this.enabled = true,
AutovalidateMode? autovalidateMode, AutovalidateMode? autovalidateMode,
}) : assert(builder != null), }) : assert(builder != null),
...@@ -331,9 +339,8 @@ class FormField<T> extends StatefulWidget { ...@@ -331,9 +339,8 @@ class FormField<T> extends StatefulWidget {
autovalidate == true && autovalidateMode == null, autovalidate == true && autovalidateMode == null,
'autovalidate and autovalidateMode should not be used together.' 'autovalidate and autovalidateMode should not be used together.'
), ),
autovalidateMode = autovalidate autovalidateMode = autovalidateMode ??
? AutovalidateMode.always (autovalidate ? AutovalidateMode.always : AutovalidateMode.disabled),
: (autovalidateMode ?? AutovalidateMode.disabled),
super(key: key); super(key: key);
/// An optional method to call with the final value when the form is saved via /// An optional method to call with the final value when the form is saved via
...@@ -385,6 +392,15 @@ class FormField<T> extends StatefulWidget { ...@@ -385,6 +392,15 @@ class FormField<T> extends StatefulWidget {
/// {@endtemplate} /// {@endtemplate}
final AutovalidateMode autovalidateMode; 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 @override
FormFieldState<T> createState() => FormFieldState<T>(); FormFieldState<T> createState() => FormFieldState<T>();
} }
......
...@@ -886,4 +886,87 @@ void main() { ...@@ -886,4 +886,87 @@ void main() {
expect(fieldValue, '123456'); expect(fieldValue, '123456');
expect(formKey.currentState.validate(), isFalse); 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<String>(
key: widgetKey,
initialValue: 'foo',
autovalidate: autovalidate,
builder: (FormFieldState<String> state) {
return Container();
},
validator: errorText,
),
),
),
),
),
);
}
// When autovalidate is true
await tester.pumpWidget(builder(autovalidate: true));
final Finder formFieldFinder = find.byKey(widgetKey);
FormField<String> 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));
});
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment