簡體   English   中英

TextFormField驗證器未顯示驗證器消息

[英]TextFormField Validator not showing validator message

我嘗試通過驗證創建此表單,因此當用戶返回每個字段時它會顯示錯誤。 但是由於某種原因,它不起作用。 我沒有理由 我現在被困住了。

這是代碼:

import 'package:flutter/material.dart';
import 'package:validate/validate.dart';

void main() => runApp(new MaterialApp(
  title: 'Forms in Flutter',
  home: new LoginForm(),
  theme: ThemeData.dark(),
));

class LoginForm extends StatefulWidget {
  String email;
  String password;
  final Function saveEmail;
  final Function savePassword;
  final Function attemptLogin;

  LoginForm({this.email, this.password, this.saveEmail, this.savePassword,
    this.attemptLogin});

  @override
  LoginFormState createState(){
    return new LoginFormState();
  }
}

class LoginFormState extends State<LoginForm> {
  final loginFormKey = GlobalKey<FormState>();
  final emailController = new TextEditingController();
  final passController = new TextEditingController();

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
        appBar: new AppBar(
          title: new Text('Login'),
        ),
        body: new Container(
          padding: new EdgeInsets.all(10.0),
          child: new Form(
            key: loginFormKey,
            child: new Column(
              children: <Widget>[
                new Row(
                  children: <Widget>[
                    new Container(
                      width: 2.0,
                      height: 18.0,
                      color: Colors.white,
                    ),
                    new Container(
                        width: 5.0,
                        height: 0.0
                    ),
                    new Expanded(child: new TextFormField(
                      decoration: new InputDecoration.collapsed(
                        hintText: "EMAIL",
                      ),

                      validator: (String value) {
                        if (!Validate.isEmail(value)) {
                          return 'Please enter Email';
                        }
                      },
                      onFieldSubmitted: (val) {
                        print(loginFormKey.currentState.validate());
                        if (loginFormKey.currentState.validate()) {
                          widget.email = val;
                          widget.saveEmail(val);
                        }
                      },
                      controller: emailController,

                    ),)
                  ],

                ),

                new Row(
                  children: <Widget>[
                    new Container(
                      width: 2.0,
                      height: 18.0,
                      color: Colors.white,
                      padding: const EdgeInsets.fromLTRB(0.0, 0.0, 5.0, 0.0),
                    ),
                    new Container(
                        width: 5.0,
                        height: 0.0
                    ),
                    new Expanded(child: new TextFormField(
                      obscureText: true,
                      decoration: new InputDecoration.collapsed(
                          hintText: 'PASSWORD',
                      ),
                      validator: (val) =>
                      val.length < 6 ?
                      'Still too short' : '',
                      onFieldSubmitted: (val) {
                        if (loginFormKey.currentState.validate()) {
                          widget.email = emailController.text;
                          print(widget.email);
                          widget.saveEmail(emailController.text);
                          widget.password = val;
                          print(widget.password);
                          widget.savePassword(val);
                          widget.attemptLogin();
                        }
                      },
                      controller: passController,
                    ),)
                  ],
                )
              ],
            ),
          ),
        )
    );
  }
}

我真的不知道是什么原因造成的。 似乎字段的onfieldSubmitted部分中的所有內容都不起作用。 如果刪除If語句,它們可以正常工作,但是一旦添加,就不會響應。

看起來很簡單,但我只是漏了一點。 任何幫助將不勝感激。 謝謝。

現在有同樣的問題。 我認為!Validate.isEmail(value)無法正常工作。 我注釋掉了,我的代碼運行良好。 嘗試編寫自己的自定義電子郵件驗證,而不是使用!Validate.isEmail(value)

單擊鍵盤上的Enter或Submit時, onFieldSubmitted屬性有效。 我認為,您應該添加用於提交的提交按鈕,因為您的驗證適用於表單,而不適用於字段或輸入。 因此,這意味着如果用戶輸入了電子郵件地址,但該用戶未輸入任何密碼,則在單擊輸入按鈕時,它將在電子郵件字段上顯示密碼驗證錯誤消息。 這不是一個好的反饋。 如果您使用提交按鈕,它應該顯示更多有關驗證消息的反饋。

// The button widget
new FlatButton(
  onPressed: () => this._submit(),
  child: new Text('Login')
);

// The submit function
void _submit() {
  if (this.loginFormKey.currentState.validate()) {
    this.loginFormKey.currentState.save();
    // Do your jobs with the validated form data.
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM