Forms 颤振中仅保存第一个表单字段验证
我面临两个问题Forms 颤振中仅保存第一个表单字段验证,forms,validation,flutter,Forms,Validation,Flutter,我面临两个问题 表单仅在第一次单击submit时记住第一个字段(此处为名称字段)的值 首次单击submit时,只有full name字段获取验证错误边界 注: 1) 第一次之后,一切都很好。 2) 验证器适用于所有人,因为我们可以看到图像清楚地显示了颤振表单中所有字段的错误 有关守则如下: void _handleRegistration() async { FormState formState = _formKey.currentState; formState.s
submit
时记住第一个字段(此处为名称字段)的值
submit
时,只有full name
字段获取验证错误边界
void _handleRegistration() async {
FormState formState = _formKey.currentState;
formState.save();
if (formState.validate()) {
// No error in validation
setState(() {
_isLoading = true;
});
...
}
更新:添加TextFormField
code:
TextFormField buildCPassFormField() {
return TextFormField(
onSaved: (newValue) => cpass = newValue,
onChanged: (value) {
if (value.isNotEmpty ||
value.length >= 6 ||
pass == cpass && errors["cpass"] != null) {
setState(() {
errors["cpass"] = "";
});
return "";
}
return null;
},
validator: (value) {
if (value.isEmpty || value == null) {
setState(() {
errors["cpass"] = kCPassNullError;
});
return "";
} else if (value.length < 6) {
setState(() {
errors["cpass"] = kShortPassError;
});
return "";
} else if (value != pass) {
setState(() {
errors["cpass"] = kMatchPassError;
});
return "";
}
setState(() {
cpass = value;
});
return null;
},
obscureText: true,
keyboardType: TextInputType.text,
autofillHints: [AutofillHints.password],
decoration: InputDecoration(
floatingLabelBehavior: FloatingLabelBehavior.always,
suffixIcon:
CustomSurffixIcon(svgIcon: "assets/icons/md-lock-closed.svg"),
labelText: 'Confirm Password',
),
);
}
TextFormField buildPassFormField() {
return TextFormField(
onSaved: (newValue) => pass = newValue,
onChanged: (value) {
if (value.isNotEmpty || value.length > 6 && errors["pass"] != null) {
setState(() {
errors["pass"] = "";
});
return "";
}
return null;
},
validator: (value) {
if (value.isEmpty || value == null) {
setState(() {
errors["pass"] = kPassNullError;
});
return "";
} else if (value.length < 6) {
setState(() {
errors["pass"] = kShortPassError;
});
return "";
}
setState(() {
pass = value;
});
return null;
},
obscureText: true,
keyboardType: TextInputType.text,
autofillHints: [AutofillHints.password],
decoration: InputDecoration(
floatingLabelBehavior: FloatingLabelBehavior.always,
suffixIcon:
CustomSurffixIcon(svgIcon: "assets/icons/md-lock-closed.svg"),
labelText: 'Password',
),
);
}
TextFormField buildEmailFormField() {
return TextFormField(
onSaved: (newValue) => email = newValue,
onChanged: (value) {
if (value.isNotEmpty ||
value != null && !emailValidatorRegExp.hasMatch(value)) {
setState(() {
errors["email"] = "";
});
return "";
}
setState(() {
email = value;
});
return null;
},
validator: (value) {
if (value.isEmpty || value == null) {
setState(() {
errors["email"] = kEmailNullError;
});
return "";
} else if (!emailValidatorRegExp.hasMatch(value)) {
setState(() {
errors["email"] = kInvalidEmailError;
});
return "";
}
return null;
},
autofocus: true,
autofillHints: [AutofillHints.email],
keyboardType: TextInputType.emailAddress,
decoration: InputDecoration(
floatingLabelBehavior: FloatingLabelBehavior.always,
suffixIcon: CustomSurffixIcon(svgIcon: "assets/icons/md-mail.svg"),
labelText: 'Email ID',
),
);
}
TextFormField buildNameFormField() {
return TextFormField(
onSaved: (newValue) => name = newValue,
onChanged: (value) {
if (value.isNotEmpty || nameValidatorRegExp.hasMatch(value)) {
setState(() {
errors["name"] = "";
});
}
setState(() {
name = value;
});
return null;
},
validator: (value) {
if (value.isEmpty || value == null) {
setState(() {
errors["name"] = kNameNullError;
});
return "";
} else if (!nameValidatorRegExp.hasMatch(value)) {
setState(() {
errors["name"] = kInvalidNameError;
});
return "";
}
return null;
},
autofocus: true,
autofillHints: [AutofillHints.name],
keyboardType: TextInputType.text,
decoration: InputDecoration(
floatingLabelBehavior: FloatingLabelBehavior.always,
suffixIcon: CustomSurffixIcon(svgIcon: "assets/icons/md-user.svg"),
labelText: 'Your Full Name',
),
);
}
TextFormField buildCPassFormField(){
返回TextFormField(
onSaved:(newValue)=>cpass=newValue,
一旦更改:(值){
if(value.isNotEmpty)||
value.length>=6||
通过==cpass&&errors[“cpass”]!=null){
设置状态(){
错误[“cpass”]=“”;
});
返回“”;
}
返回null;
},
验证器:(值){
if(value.isEmpty | | value==null){
设置状态(){
错误[“cpass”]=kCPassNullError;
});
返回“”;
}else if(value.length<6){
设置状态(){
错误[“cpass”]=kShortPassError;
});
返回“”;
}else if(值!=通过){
设置状态(){
错误[“cpass”]=kMatchPassError;
});
返回“”;
}
设置状态(){
cpass=价值;
});
返回null;
},
蒙昧文字:对,
键盘类型:TextInputType.text,
自动填充提示:[自动填充提示.密码],
装饰:输入装饰(
浮动LabelBehavior:浮动LabelBehavior.always,
足够:
CustomSurffixIcon(svgIcon:“assets/icons/md lock closed.svg”),
labelText:“确认密码”,
),
);
}
TextFormField buildPassFormField(){
返回TextFormField(
onSaved:(newValue)=>pass=newValue,
一旦更改:(值){
if(value.isNotEmpty | | value.length>6&&errors[“pass”]!=null){
设置状态(){
错误[“通过”]=“”;
});
返回“”;
}
返回null;
},
验证器:(值){
if(value.isEmpty | | value==null){
设置状态(){
错误[“通过”]=kPassNullError;
});
返回“”;
}else if(value.length<6){
设置状态(){
错误[“通过”]=kShortPassError;
});
返回“”;
}
设置状态(){
通过=值;
});
返回null;
},
蒙昧文字:对,
键盘类型:TextInputType.text,
自动填充提示:[自动填充提示.密码],
装饰:输入装饰(
浮动LabelBehavior:浮动LabelBehavior.always,
足够:
CustomSurffixIcon(svgIcon:“assets/icons/md lock closed.svg”),
labelText:“密码”,
),
);
}
TextFormField buildEmailFormField(){
返回TextFormField(
onSaved:(newValue)=>email=newValue,
一旦更改:(值){
if(value.isNotEmpty)||
value!=null&!emailValidatorRegExp.hasMatch(值)){
设置状态(){
错误[“电子邮件”]=“”;
});
返回“”;
}
设置状态(){
电子邮件=价值;
});
返回null;
},
验证器:(值){
if(value.isEmpty | | value==null){
设置状态(){
错误[“电子邮件”]=kEmailNullError;
});
返回“”;
}如果(!emailValidatorRegExp.hasMatch(值))为else{
设置状态(){
错误[“电子邮件”]=KinvalideMailer;
});
返回“”;
}
返回null;
},
自动对焦:对,
自动填充提示:[自动填充提示.电子邮件],
键盘类型:TextInputType.emailAddress,
装饰:输入装饰(
浮动LabelBehavior:浮动LabelBehavior.always,
后缀:CustomSurffixIcon(svgIcon:“assets/icons/md mail.svg”),
labelText:“电子邮件ID”,
),
);
}
TextFormField buildNameFormField(){
返回TextFormField(
onSaved:(newValue)=>name=newValue,
一旦更改:(值){
if(value.isNotEmpty | | nameValidatorRegExp.hasMatch(value)){
设置状态(){
错误[“名称”]=“”;
});
}
设置状态(){
名称=值;
});
返回null;
},
验证器:(值){
if(value.isEmpty | | value==null){
设置状态(){
错误[“名称”]=kNameNullError;
});
返回“”;
}如果(!nameValidatorRegExp.hasMatch(值)){
设置状态(){
错误[“名称”]=kInvalidNameError;
});
返回“”;
}
返回null;
},
自动对焦:对,
自动填充提示:[自动填充提示.名称],
键盘类型:TextInputType.text,
装饰:输入装饰(
浮动LabelBehavior:浮动LabelBehavior.always,
后缀:CustomSurffixIcon(svgIcon:“assets/icons/md user.svg”),
labelText:“您的全名”,
),
);
}
一起分享代码,以便人们能更好地帮助您。@CaptainxGod您想让我添加TextFormField
代码吗?