1

How to stick validation text inside TextFormField. By default it goes below

the text field

I want like this I want like this

But it shows like this

But it shows like this

I have looked in various sources, but did not find a suitable answer

is there any way to show the validation text inside the textbox

S.A.R
  • 47
  • 6

2 Answers2

1

You can update text from TextEditingController if validation fails for a certain text field and also can remove text from controller in "onTap" property.

TextEditingController _passwordController = TextEditingController();
if(condition)
{
 //success call
}
else
{
setState((){
  _passwordController.text="Password Does not match
 });
}
  • Thanks for the answer. then the text in the field will simply change, you need how the validation would work, but that the validation text would be inside the text field – S.A.R Jun 06 '21 at 20:21
  • You can simply remove the text by using 'onTap' property. `setState((){ _passwordController.clear(); });` – Prabhanshu Tiwari Jun 06 '21 at 20:34
0

you should validate your form in the Following way

class MyForm extends StatefulWidget {
  @override
  MyFormState createState() {
    return MyFormState();
  }
}

// Create a corresponding State class.
// This class holds data related to the form.
class MyFormState extends State<MyForm> {
  // Create a global key that uniquely identifies the Form widget
  // and allows validation of the form.
  //
  // Note: This is a GlobalKey<FormState>,
  // not a GlobalKey<MyFormState>.
  final _formKey = GlobalKey<FormState>();

  @override
  Widget build(BuildContext context) {
    // Build a Form widget using the _formKey created above.
    return Form(
      key: _formKey,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          TextFormField(
            // The validator receives the text that the user has entered.
            validator: (value) {
              if (value == null || value.isEmpty) {
                return 'Please enter some text';
              }
              return null;
            },
          ),
          Padding(
            padding: const EdgeInsets.symmetric(vertical: 16.0),
            child: ElevatedButton(
              onPressed: () {
                // Validate returns true if the form is valid, or false otherwise.
                if (_formKey.currentState!.validate()) {
                  // If the form is valid, display a snackbar. In the real world,
                  // you'd often call a server or save the information in a database.
                  // sendData();
                  ScaffoldMessenger.of(context)
                      .showSnackBar(SnackBar(content: Text('Processing Data')));
                }
              },
              child: Text('Submit'),
            ),
          ),
        ],
      ),
    );
  }
}
Mehran Ullah
  • 550
  • 4
  • 17
  • Thanks for the answer. it just shows a snackbar, you need how the validation works, but for the validation text to be inside the text field – S.A.R Jun 06 '21 at 20:23