-
Notifications
You must be signed in to change notification settings - Fork 30.2k
Validation with dependency between fields did not happen as expected #74765
Copy link
Copy link
Closed
Description
Steps to Reproduce
- Run the code below
- Click the validate button
- Uncheck the "Field Is Required" checkbox
code sample
import 'package:flutter/material.dart';
void main() {
runApp(
MaterialApp(
home: MyWidget(),
),
);
}
class MyWidget extends StatefulWidget {
@override
_MyWidgetState createState() => _MyWidgetState();
}
class _MyWidgetState extends State<MyWidget> {
GlobalKey<FormState> _formKey;
bool _required = true;
@override
void initState() {
super.initState();
_formKey = GlobalKey<FormState>();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Form(
key: _formKey,
child: Column(
children: [
CheckboxListTile(
title: const Text('Field Is Required'),
value: _required,
onChanged: (value) {
setState(() {
_required = value;
});
},
),
RaisedButton(
child: const Text('Validate'),
onPressed: () {
_formKey.currentState.validate();
},
),
TextFormField(
autovalidateMode: AutovalidateMode.onUserInteraction,
decoration: const InputDecoration(labelText: 'Field 1'),
validator: (value) {
if (value == '' && _required) {
return 'Error';
}
return null;
},
),
],
),
),
);
}
}Expected results: As the field looks like AutovalidateMode.onUserInteraction I expected the field to be revalidated and the error removed.
I had to create another component for the field to self-validate, something like _hasInteractedByUser = true; in the block below would solve
flutter/packages/flutter/lib/src/widgets/form.dart
Lines 459 to 464 in 357dcc9
| bool validate() { | |
| setState(() { | |
| _validate(); | |
| }); | |
| return !hasError; | |
| } |
Actual results: As the field has not been changed by the user, the field is not revalidated.
flutter doctor -v
[√] Flutter (Channel stable, 1.22.5, on Microsoft Windows [Version 10.0.19042.746], locale pt-BR)
• Flutter version 1.22.5 at C:\src\flutter
• Framework revision 7891006299 (7 weeks ago), 2020-12-10 11:54:40 -0800
• Engine revision ae90085a84
• Dart version 2.10.4
[√] Android toolchain - develop for Android devices (Android SDK version 30.0.0-rc1)
• Android SDK at D:\AndroidSdk
• Platform android-30, build-tools 30.0.0-rc1
• ANDROID_HOME = D:\AndroidSdk
• ANDROID_SDK_ROOT = D:\AndroidSdk
• Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
• Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)
• All Android licenses accepted.
[!] Android Studio (version 4.1.0)
• Android Studio at C:\Program Files\Android\Android Studio
X Flutter plugin not installed; this adds Flutter specific functionality.
X Dart plugin not installed; this adds Dart specific functionality.
• Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)
[!] VS Code (version 1.52.1)
• VS Code at C:\Users\erli\AppData\Local\Programs\Microsoft VS Code
X Flutter extension not installed; install from
https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter
[√] Connected device (1 available)
• SM M315F (mobile) • RX8N80DL2NK • android-arm64 • Android 10 (API 29)
! Doctor found issues in 2 categories.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels