The new Form, FormField auto-validation API
Summary
#The previous auto validation API for the Form
and
FormField
widgets didn't control when auto validation
should occur. So the auto validation for these widgets
always happened on first build when the widget was first
visible to the user, and you weren't able to control
when the auto validation should happen.
Context
#Due to the original API not allowing developers to change
the auto validation behavior for validating only when
the user interacts with the form field, we added new API
that allows developers to configure how they want
auto validation to behave for the Form
and FormField
widgets.
Description of change
#The following changes were made:
- The
autovalidate
parameter is deprecated. - A new parameter called
autovalidateMode
, an Enum that accepts values from theAutovalidateMode
Enum class, is added.
Migration guide
#To migrate to the new auto validation API you need to
replace the usage of the deprecated autovalidate
parameter to the new autovalidateMode
parameter.
If you want the same behavior as before you can use:
autovalidateMode = AutovalidateMode.always
.
This makes your Form
and FormField
widgets auto
validate on first build and every time it changes.
Code before migration:
class MyWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return FormField(
autovalidate: true,
builder: (FormFieldState state) {
return Container();
},
);
}
}
Code after migration:
class MyWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return FormField(
autovalidateMode: AutovalidateMode.always,
builder: (FormFieldState state) {
return Container();
},
);
}
}
Timeline
#Landed in version: 1.21.0-5.0.pre
In stable release: 1.22
References
#API documentation:
Relevant issues:
Relevant PRs:
- PR 56365: FormField should autovalidate only if its content was changed
- PR 59766: FormField should autovalidate only if its content was changed (fixed)
除非另有说明,本文档之所提及适用于 Flutter 的最新稳定版本,本页面最后更新时间: 2024-10-09。 查看文档源码 或者 为本页面内容提出建议。