Issue
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?
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. 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.
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
// });
// },
),
),
),
),
Solution
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();
................
}
Answered By - Tasnuva oshin
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.