【发布时间】:2021-04-23 13:07:24
【问题描述】:
我正在尝试验证具有电子邮件和密码字段的登录表单。验证模式设置为onUserInteraction。
但当前的行为,即使开始在电子邮件字段上输入,也会验证密码字段。 。 我想要实现的是
-
验证单个文本字段。不在一起
-
也许,仅在对焦后验证(模糊)
Form( autovalidateMode: AutovalidateMode.onUserInteraction, onChanged: () { controller.isValidForm.value = controller.formKey.currentState.validate(); }, key: controller.formKey, child: Column( children: <Widget>[ FormTextField( validator: (value) { if (value.isEmpty) { return 'Please enter an email address'; } else if (!controller.emailRegExp.hasMatch(value)) { return 'Invalid email address!'; } return null; }, onSaved: (value) { controller.model.emailAddress = value; }, title: "Email id", ), UIHelper.verticalSpaceLarge(), FormTextField( isObscure: controller.isPasswordObsecured.value, validator: (value) { if (value.isEmpty) { return 'Please enter a password'; } return null; }, onSaved: (value) { controller.model.password = value; }, title: "Password", isObscureCallBack: controller.changePasswordVisibility, isPasswordField: true, ), UIHelper.verticalSpaceExtraLarge(), FormSubmitButton( isValidForm: controller.isValidForm.value, onPressed: () { if (controller.formKey.currentState.validate()) { controller.formKey.currentState.save(); print(controller.model); controller.doLogin(); } }, label: "Login", ), ], ), ),
【问题讨论】:
-
这是因为你在 Form 上使用了 onChanged。
-
尝试删除
onChanged: () { controller.isValidForm.value = controller.formKey.currentState.validate(); }, -
如果你想继续使用这种风格,那么你将不得不制作 2 个不同的键。
-
删除 onChange 对验证行为没有影响 @UjjwalRaijada
-
验证器仅在被调用时才起作用。它在 onChange 和 FormSubmitButton 上被调用。请尝试删除 onChange 并使用热重载。它应该工作/
标签: flutter flutter-getx