文本表单字段验证器错误消息未显示

超级植物

我期望TextFormFields中的验证者显示错误消息,如果其中多个验证者之一返回字符串。但是,尽管已实现它们,但即使在满足条件之后,它们仍会以某种方式无法显示错误消息(例如value.length> = 3)。

                      Form(
                        key: formKey,
                        child: Column(
                          children: <Widget>[
                            TextFormField(
                              // validator fails to display its error message
                              validator: (value) => value.length >= 3 ? null : "Username must consist of at least 3 characters.",
                              decoration: InputDecoration(
                                filled: true,
                                fillColor: Colors.black12,
                                hintText: "Username *",
                                hintStyle: hintTextStyle,
                                border: OutlineInputBorder(
                                  borderRadius: BorderRadius.circular(15.0),
                                )
                              ),
                              maxLines: 1,
                              controller: usernameController
                            ),
                            SizedBox(height: 20.0),
                            TextFormField(
                              // validator fails to display its error message
                              validator: (value) {
                                return RegExp(
                                    r"^[a-zA-Z0-9.a-zA-Z0-9.!#$%&'*+-/=?^_`{|}~]+@[a-zA-Z0-9]+\.[a-zA-Z]+"
                                ).hasMatch(value) ? null : "Please enter a valid email address.";
                              },
                              decoration: InputDecoration(
                                  filled: true,
                                  fillColor: Colors.black12,
                                  hintText: "Email *",
                                  hintStyle: hintTextStyle,
                                  border: OutlineInputBorder(
                                      borderRadius: BorderRadius.circular(15.0),
                                  )
                              ),
                              maxLines: 1,
                              controller: emailController
                            ),
                            SizedBox(height: 20.0),
                            TextFormField(
                              // validator fails to display its error message
                              validator: (value) => value.length >= 6 ? null : "Password must be at least 6 characters and contains both letter and number.",
                              decoration: InputDecoration(
                                  filled: true,
                                  fillColor: Colors.black12,
                                  hintText: "Password *",
                                  hintStyle: hintTextStyle,
                                  border: OutlineInputBorder(
                                      borderRadius: BorderRadius.circular(15.0),
                                  )
                              ),
                              maxLines: 1,
                              obscureText: true,
                              controller: passwordController
                            ),
                            SizedBox(height: 20.0),
                            TextFormField(
                              // validator fails to display its error message
                              validator: (value) => value == passwordController.text ? null : "Password does not match.",
                              decoration: InputDecoration(
                                filled: true,
                                fillColor: Colors.black12,
                                hintText: "Repeat password *",
                                hintStyle: hintTextStyle,
                                border: OutlineInputBorder(
                                  borderRadius: BorderRadius.circular(15.0),
                                )
                              ),
                              maxLines: 1,
                              obscureText: true,
                              controller: repeatPasswordController
                            ),
                            Container(
                                margin: EdgeInsets.symmetric(vertical: 20.0),
                                width: double.infinity,
                                child: RaisedButton(
                                  padding: EdgeInsets.symmetric(vertical: 20.0),
                                  shape: RoundedRectangleBorder(
                                    borderRadius: BorderRadius.circular(15.0),
                                  ),
                                  color: Colors.blueAccent,
                                  child: Text("Sign Up", style: Theme.of(context).textTheme.bodyText1),
                                  onPressed: () => signUpCurrentUser(emailController.text, passwordController.text),
                                )
                            )
                            // irrelevant widgets goes below..

这是注册功能

signUpCurrentUser(String email, String password) {
    authentication.signUp(email, password).then((val) {
      if(formKey.currentState.validate()) {
        setState(() => isLoading = true);

        if(val != null)
          Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) => HomeScreen()));
        else
          setState(() => isLoading = false);
      }
    });
  }

以下是如果我在TextFormFields保留为空的情况下按“ Sign Up”按钮时在控制台中返回的内容,但是我认为这与错误无关。

Performing hot reload...
Syncing files to device iPhone 11 Pro Max...
Reloaded 4 of 513 libraries in 355ms.
[VERBOSE-2:ui_dart_state.cc(157)] Unhandled Exception: PlatformException(ERROR_WEAK_PASSWORD, The password must be 6 characters long or more., null)
#0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:569:7)
#1      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:156:18)
<asynchronous suspension>
#2      MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:329:12)
#3      MethodChannel.invokeMapMethod (package:flutter/src/services/platform_channel.dart:356:48)
#4      MethodChannelFirebaseAuth.createUserWithEmailAndPassword (package:firebase_auth_platform_interface/src/method_channel_firebase_auth.dart:64:23)
#5      FirebaseAuth.createUserWithEmailAndPassword (package:firebase_auth/src/firebase_auth.dart:64:10)
#6      AuthMethods.signUp (package:apui/services/authentication.dart:8:39)
#7      _SignupScreenState.signUpCurrentUser (package:apui/screens/signup_screen.dart:27:20)
#8      _SignupScreenStat<…>
Application finished.
沙伯·谢克

需要调用formKey.currentState.validate()您的注册 按钮 onPressed功能。

示例代码段如下:

import 'package:flutter/material.dart';

class DummyPage extends StatefulWidget {
  @override
  _DummyPageState createState() => _DummyPageState();
}

class _DummyPageState extends State<DummyPage> {
  TextEditingController usernameController = TextEditingController();
  GlobalKey<FormState> formKey = GlobalKey<FormState>();
  @override
  Widget build(BuildContext context) {
    return Material(
      child: Container(
        child: Form(
          key: formKey,
          child: Column(
            children: <Widget>[
              TextFormField(
                // validator fails to display its error message
                validator: (value) => value.length >= 3
                    ? null
                    : "Username must consist of at least 3 characters.",
                decoration: InputDecoration(
                    filled: true,
                    fillColor: Colors.black12,
                    hintText: "Username *",
                    // hintStyle: hintTextStyle,
                    border: OutlineInputBorder(
                      borderRadius: BorderRadius.circular(15.0),
                    )),
                maxLines: 1,
                controller: usernameController,
              ),
              RaisedButton(
                child: Text('Submit'),
                onPressed: () {
                  if (formKey.currentState.validate()) {
                    print(usernameController.text);
                  }
                },
              )
            ],
          ),
        ),
      ),
    );
  }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

缺少必需表单字段的Symfony验证错误消息

来自分类Dev

表单字段未显示

来自分类Dev

django表单字段验证和错误显示

来自分类Dev

MVC表单字段的验证消息的位置

来自分类Dev

MVC表单字段的验证消息的位置

来自分类Dev

Rails表单验证未显示错误消息,并使用空白字段

来自分类Dev

Django更改表单字段错误消息

来自分类Dev

Django表单字段验证错误

来自分类Dev

PDF表单字段内容未显示

来自分类Dev

Django表单字段未显示

来自分类Dev

表单验证对所有字段显示相同的错误消息

来自分类Dev

表单验证错误消息未显示在ModelForm中

来自分类Dev

表单视图中未显示验证错误消息

来自分类Dev

发生验证错误时,Symfony2表单字段未更新

来自分类Dev

如何在AngularJS中显示数组表单字段的验证错误?

来自分类Dev

Symfony2表单错误未显示,UniqueEntity错误消息未显示在相应字段旁边

来自分类Dev

Spring MVC验证器中未显示错误消息

来自分类Dev

Spring MVC验证器中未显示错误消息

来自分类Dev

如何从表单字段返回多个错误消息

来自分类Dev

Rails错误表单字段验证CSS和语义UI

来自分类Dev

django表单字段未显示在模板中-为什么?

来自分类Dev

HTML 表单字段未显示触摸状态 - Angular 7

来自分类Dev

表单字段验证条件

来自分类Dev

文本编辑器中的PDF表单字段

来自分类Dev

yii2 gridview搜索字段未触发验证,未显示错误消息

来自分类Dev

在表单数组 angular 中验证和显示特定表单控制器的错误消息

来自分类Dev

错误消息未显示Scala播放表单

来自分类Dev

CSS显示表单字段

来自分类Dev

如何使PHP验证表单中的下拉菜单显示“ *必填字段”错误消息?

Related 相关文章

热门标签

归档