简体   繁体   English

TextFormField 下的错误文本不起作用

[英]Error Text under TextFormField not working

I would like to have the error text appear under the user input container, but I can't even get the error to appear under the text.我想让错误文本出现在用户输入容器下,但我什至无法让错误出现在文本下。 What am I doing wrong with the Validator?我在验证器上做错了什么?

Here's what my form looks like这是我的表格的样子

我的应用截图

I am trying to get a 2nd password field to show an error message if the user input does not match the 1st password field.如果用户输入与第一个密码字段不匹配,我正在尝试获取第二个密码字段以显示错误消息。 I have the TextFormFields wrapped in a container with decoration.我将 TextFormFields 包装在带有装饰的容器中。 I tried removing the decoration and using the TextFormField validator, but I still can't get the error message to appear below the user input or the container.我尝试删除装饰并使用 TextFormField 验证器,但我仍然无法让错误消息出现在用户输入或容器下方。

My Code我的代码

child: Container(
                      height: 50,
                      width: MediaQuery.of(context).size.width / 1.5,
                      decoration: BoxDecoration(
                        color: Colors.white,
                        borderRadius: const BorderRadius.only(
                            topLeft: Radius.circular(10),
                            topRight: Radius.circular(10),
                            bottomLeft: Radius.circular(10),
                            bottomRight: Radius.circular(10)),
                        boxShadow: [
                          BoxShadow(
                            color: Colors.grey.withOpacity(0.5),
                            spreadRadius: 2,
                            blurRadius: 2,
                          ),
                        ],
                      ),
                      child: Form(
                        key: _formKey,
                        child: Padding(
                          padding:
                              const EdgeInsets.only(right: 10.0, left: 10.0),
                          child: TextFormField(
                            obscureText: true,
                            validator: (value) {
                              if (value!.isEmpty) {
                                return "Please Re-Enter New Password";
                              } else if (value.length < 6) {
                                return "Password must be atleast 6 characters long";
                              } else if (value != _password) {
                                return 'Passwords do not match';
                              } else {
                                // _confirmPassword = value;
                                return null;
                              }
                            },
                            decoration: const InputDecoration(
                              border: InputBorder.none,
                              focusedBorder: InputBorder.none,
                              enabledBorder: InputBorder.none,
                              errorBorder: InputBorder.none,
                              disabledBorder: InputBorder.none,
                              hintText: 'Confirm Password',
                              // helperText: 'test1',
                              // errorText: 'test2',
                            ),

                            // onChanged: (value) {
                            //   setState(() {
                            //     _confirmPassword = value
                            //       .trim(); // gets rid of leading and trailing spaces
                            //   });
                            // },
                          ),
                        ),
                      ),
                    ),

Check this validation example you will have a better idea about error handling and it will show error message.检查此验证示例,您将对错误处理有更好的了解,它将显示错误消息。


     String validateName(String value) {
        String patttern = r'(^[a-zA-Z ]*$)';
        RegExp regExp = new RegExp(patttern);
        if (value.length == 0) {
          return "Name is Required";
        } else if (!regExp.hasMatch(value)) {
          return "Name must be a-z and A-Z";
        }
        return null;
      }
    
      TextFormField(
                                  controller: _lastname, validator: validateName ,
                                  //initialValue: widget.contact.last_name,
                                  decoration:
                                      InputDecoration(labelText: 'Last name'),
                                ),
    
    void Save() {
     if (_keyForm.currentState.validate()) {
          // No any error in validation
          _keyForm.currentState.save(); 
    ................
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM