Flutter 颤振:表单验证一起验证所有文本字段
我正在尝试验证带有电子邮件和密码字段的登录表单。验证模式设置为Flutter 颤振:表单验证一起验证所有文本字段,flutter,flutter-getx,Flutter,Flutter Getx,我正在尝试验证带有电子邮件和密码字段的登录表单。验证模式设置为onUserInteraction 但当前的行为是,即使在开始键入电子邮件字段时,也会验证密码字段。 . 我想要达到的是 验证单个文本字段。不是全部 可能,仅在调焦(模糊)后验证 表单( autovalidateMode:autovalidateMode.onUserInteraction, 一旦更改:(){ controller.isValidForm.value= controller.formKey.currentState.
onUserInteraction
但当前的行为是,即使在开始键入电子邮件字段时,也会验证密码字段。
.
我想要达到的是
表单(
autovalidateMode:autovalidateMode.onUserInteraction,
一旦更改:(){
controller.isValidForm.value=
controller.formKey.currentState.validate();
},
key:controller.formKey,
子:列(
儿童:[
FormTextField(
验证器:(值){
if(value.isEmpty){
返回“请输入电子邮件地址”;
}如果(!controller.emailRegExp.hasMatch(值)){
返回“无效的电子邮件地址!”;
}
返回null;
},
已保存:(值){
controller.model.emailAddress=值;
},
标题:“电子邮件id”,
),
UIHelper.verticalSpaceLarge(),
FormTextField(
isObscure:controller.isPasswordObsecured.value,
验证器:(值){
if(value.isEmpty){
返回“请输入密码”;
}
返回null;
},
已保存:(值){
controller.model.password=值;
},
标题:“密码”,
isObscureCallBack:controller.changePasswordVisibility,
isPasswordField:对,
),
UIHelper.verticalSpaceExtraLarge(),
表单提交按钮(
isValidForm:controller.isValidForm.value,
已按下:(){
if(controller.formKey.currentState.validate()){
controller.formKey.currentState.save();
打印(controller.model);
controller.doLogin();
}
},
标签:“登录”,
),
],
),
),
继续我们的谈话。。。我要做的是,我创建了一个函数,在提交表单时调用该函数。这是检查验证并显示错误(如果有)的时间。 我要验证的代码:
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
}
这是因为您正在表单上使用onChanged。请尝试删除
onChanged:(){controller.isValidForm.value=controller.formKey.currentState.validate();},
如果要继续使用此样式,然后必须生成两个不同的键。删除onChange不会影响验证行为@UjjwalRaijadavalidator仅在调用它时有效。它在onChange和FormSubmitButton上被调用。请尝试删除onChange并使用热重新加载。应该行得通/
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
}