Skip to content

Instantly share code, notes, and snippets.

@b-cancel
Created September 4, 2018 06:30
Show Gist options
  • Save b-cancel/ad61d8dd2f0ad7ef1425199b1cbb223a to your computer and use it in GitHub Desktop.
Save b-cancel/ad61d8dd2f0ad7ef1425199b1cbb223a to your computer and use it in GitHub Desktop.
FLUTTER => password and confirm password field validation before the creation of a user. Handles all edge cases well without blocking user input at any point.
String getPasswordValidationError(bool forPassword) {
String initialPasswordString = focusNodeToValue[passwordFocusNode].value;
String confirmPasswordString = focusNodeToValue[confirmPasswordFocusNode].value;
///-----make sure this particular password is valid
if (forPassword) {
if (initialPasswordString.isNotEmpty == false)
return "Password Required";
else if (initialPasswordString.length < 6)
return "The Password Requires 6 Characters Or More";
} else {
if (confirmPasswordString.isNotEmpty == false)
return "Password Required";
else if (confirmPasswordString.length < 6)
return "The Password Requires 6 Characters Or More";
}
//Note: we don't check our counter part here because we assume that either
//1. It has yet to be filled out
// - so we don't scare our user with red
//2. It has been filled out...
// 2a. and it has its own individual error
// - where its implicit the passwords don't match because it didn't even pass its individual tests
// - much less match up to us that did pass our individual tests [because otherwise we would have returned by now]
// - consequently, showing individual error reveals more than just saying that the passwords don't match
// 2b. and it does not have it own individual error
// - so now it must be checked against us
///-----make sure both passwords are valid together
if (initialPasswordString.isNotEmpty && confirmPasswordString.isNotEmpty) {
if (initialPasswordString != confirmPasswordString){
//this particular case means that we are valid... but it only says that our counter part is not empty
//so this revels 2 cases for our counter part
// 1. It doesn't meet all of its individual tests
// - in which case as explained above, the individual error should stay because its more descriptive
// 2. It does meet all of its individual tests
// - in which case it might be best to also indicate in its field that the passwords don't match
if(forPassword && focusNodeToError[confirmPasswordFocusNode].value == null){
focusNodeToError[confirmPasswordFocusNode].value = "The Passwords Don't Match";
}
else if(focusNodeToError[passwordFocusNode].value == null){
focusNodeToError[passwordFocusNode].value = "The Passwords Don't Match";
}
return "The Passwords Don't Match";
}
else {
//this particular case means that we are valid... and our counter part is valid... and it matches us
//however although our error will be cleared out, our counter part might have had an error and it has to be cleared out too
if(forPassword) focusNodeToError[confirmPasswordFocusNode].value = null;
else focusNodeToError[passwordFocusNode].value = null;
return null;
}
} else {
//this particular case means that we are valid... but our counter part is empty
//our counter part can be empty for 2 reasons
// 1. it was never filled out
// - in which case we don't want to scare our users with red
// 2. it was filled out and erased
// - in which case the individual error will already be shown
return null;
}
}
String getInitialPasswordValidationError() => getPasswordValidationError(true);
String getConfirmPasswordValidationError() => getPasswordValidationError(false);
@ragenmah
Copy link

ragenmah commented Sep 3, 2020

Error Occured : Undefined name focusNodeToError
How can i solve this error?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment