我正在尝试验证具有电子邮件和密码字段的登录表单。验证模式设置为 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",
),
],
),
),
答案 0 :(得分:0)
继续我们的对话……我所做的是,我创建了一个在提交表单时调用的函数。那是检查验证的时间并显示错误(如果有)。 我要验证的代码:
void _submit() async {
final isValid = _form.currentState!.validate(); // validation
if (!isValid) { // if validation is false it will return error as per mentioned by you in validator
return;
}
_form.currentState!.save(); // if everything is fine, it will save the form and proceed further
// code from here if the form gets validated
}