Skip to content

TextFormField Validator not showing validator message #17226

@bowojori7

Description

@bowojori7

I tried to create this form with validation, so it shows the errors when the user returns each field. But for some reason it doesn't work. I have no reason why. I'm just stuck now.

Here's the code:

    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,
                        ),)
                      ],
                    )
                  ],
                ),
              ),
            )
        );
      }
    }

I really don't know what's causing this. It seems like everything in the onFieldSubmitted part of the fields don't work. If I remove the If statements, they work okay, but once it's added it gives no response.

Seems like something simple but I'm just missing the point. Any help would be greatly appreciated. Thanks.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions