Remove error message in TextFormField in Flutter - flutter

Hi I want to remove error message caused by validator in TextFormField widget after I submit a form in flutter.
Ps : I want the error message disappeared after I select again the TextFormField to write into it again.
TextFormField(
controller: _emailController,
keyboardType: TextInputType.emailAddress,
textInputAction: TextInputAction.next,
validator: (value) {
if (value == null ||
value.isEmpty ||
!EmailValidator.validate(value)) {
return nullEmailMsg;
}
return null;
},
decoration: InputDecoration(
hintText: emailHint,
prefixIcon: Icon(Icons.mail),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
),
),
),

If you put your TextFormField inside a Form with the property autovalidateMode: AutovalidateMode.onUserInteraction, the validation function should trigger when you select again or change the text.
If the validation function is satisfied, the error message will be removed.

You have to use the autoValidateMode method in TextFormField.
Here, is the some of the reference:
https://api.flutter.dev/flutter/widgets/AutovalidateMode-class.html

Related

how to change color on TextFormField validator: (value) => validateCharacters(value),

I want to change this color to grey But but its red How can I achieve that or is it possible or not?
code:
TextFormField(
autovalidateMode: AutovalidateMode.onUserInteraction,
//Want to change color HERE
validator: (value) => validateCharacters(value),
inputFormatters: [
LengthLimitingTextInputFormatter(20),
],
},
),
String? validateCharacters(String? value) {
String pattern = (r'^(?=.*?)(?=.*?[a-z])(?=.*?[20]).{20,}$');
RegExp regex = RegExp(pattern);
if (value == null || value.isEmpty || !regex.hasMatch(value)) {
return 'note: `Only 20 characters allow`'; //Want this to be grey color
} else {
return null;
}
}
You can add a error style inside decoration.
Check this:
TextFormField(
autovalidateMode: AutovalidateMode.onUserInteraction,
validator: (value) => validateCharacters(value),
decoration: InputDecoration(
errorStyle: TextStyle(color: Colors.grey)
),
inputFormatters: [
LengthLimitingTextInputFormatter(20),
],
},
),
If we want to change validation text color then put in to the decoration property in error style according to your UI.
TextFormField( autovalidateMode: AutovalidateMode.onUserInteraction, validator: (value) => validateCharacters(value), decoration: enter code hereInputDecoration(errorStyle: TextStyle(color: Colors.grey)),)
`
You have to use
decoration: InputDecoration(
errorStyle: TextStyle(color: Colors.red)
),
in your TextFormField

Flutter | How to show a Hint Text when a value is entered

I want the Hint Text to remain even when Text is entered in the TextField:
But if I give TextField something like "hintText:" in simple way, Hint Text disappears when TextField is entered:
What should I do so that the hintText doesn't disappear even when a value is entered in the TextField?
.
.
.
I tried the following:
I. I tried using the suffix widget. But it appears from the end of the TextField. (If it was possible to make the suffix widget appear after the text, I think the problem would have been solved.) :
II. Obviously Prefix Widget can't help here :
Any answers are welcome, thanks.
You can use label to be visible at the top of the TextField & the hint u can simply add below line:
floatingLabelBehavior: FloatingLabelBehavior.always
full example is given below
TextFormField(
controller: textController,
style: theme.textTheme.bodyText2,
keyboardType: keyboardType ?? TextInputType.number,
enableInteractiveSelection: false,
decoration: InputDecoration(
labelText: labelText,
labelStyle: theme.textTheme.headline6,
suffixText: suffixText ?? '',
border: OutlineInputBorder(
borderSide:
BorderSide(color: theme.textTheme.bodyText2.color, width: 2),
),
hintText: '0.0',
floatingLabelBehavior: FloatingLabelBehavior.always),
validator: (value) {
if (value.isEmpty) {
return 'Please enter some text';
}
return null;
},
onChanged: (String text) => onChange(text),
);

Showing an error under a TextField in Flutter

Material design allows a text field to indicate an error via a small red label under the input box: https://material.io/components/text-fields (see screenshot below).
Is there a way to achieve this for a TextField field in Flutter? I expected this to be a property of either TextField or TextEditingController, but haven't found anything like it.
It is present in the decoration property in TextField, also you can style it using it's style property.
TextField(
decoration: InputDecoration(
errorStyle: TextStyle(),
errorText: 'Please enter something'
),
),
You show errors based on the validation results which are returned by the validator function provided by TextFormField, You check for some conditions there and return an error message or null based on what you want to show and when, or if you don't want to show anything.
child: new TextFormField(
autocorrect: false,
validator: (value) {
if (value.isEmpty) {
return 'Error Message';
}
return null;
},
onSaved: (val) => //do something...,
decoration: new InputDecoration(labelText: "Label*"),
),

Sanitize TextFormField value before validation in flutter

I want to use do some sanitization like trimming a string before the validator function is called. I don't want to do it in the onChange method but wait until the user finsehes his input.
TextFormField(
decoration: InputDecoration(
labelText: 'Email*', hintText: "john.doe#gmail.com"),
keyboardType: TextInputType.emailAddress,
validator: emailValidator,
onChanged: (String str) {
}
),

How to remove validation when a form is closed flutter

In Flutter, I have a form with a textformfield. In the textFormField, I have a validator:
validator: (val) => val.length == 0 ? 'description' : null,
TextFormField(
decoration: new InputDecoration(
labelText: 'Description',
border: InputBorder.none,
focusedBorder: InputBorder.none,
),
controller: controllerTitle,
maxLines: null,
keyboardType: TextInputType.multiline,
validator: (val) => val.length == 0 ? 'description' : null,
onSaved: (val) => title = val,
),
I have a popup form with a button 'close' and 'push'. When the form is empty and I press the push button validation triggers. Once I pressed the close button just after the push button a form closes and when I open a form one more time I can see this validation text in red, How can I clear my validation when the form is closed.
Try adding
setState(() {});
method to close button.