Skip to content

Instantly share code, notes, and snippets.

@iammart
Forked from xvarlez/ProfileActivity.java
Created June 1, 2018 09:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iammart/6edf0d605c2cad172d69ead9f4f8ffeb to your computer and use it in GitHub Desktop.
Save iammart/6edf0d605c2cad172d69ead9f4f8ffeb to your computer and use it in GitHub Desktop.
Android simple user input validation system, with support for EditText, AutoCompleteTextView, TextInputLayout
/**
* The activity containing your EditTexts.
*/
public class ProfileActivity extends AppCompatActivity {
/**
* Input field "your name".
*/
@Bind(R.id.edittext_name)
EditText nameEditText;
/**
* Input field "mobile number".
*/
@Bind(R.id.edittext_mobile_number)
EditText mobileNumberEditText;
/**
* Activity-specific user input validator.
*/
ProfileInputValidator inputValidator;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
ButterKnife.bind(this);
}
/**
* Action when the "validate" button is pressed.
*/
@OnClick(R.id.button_profile_validate)
public void validateButtonClick() {
if (getInputValidator().validateUserInput()) {
// The EditTexts are filled as wanted
}
}
// --------------------------- VALIDATOR PART ---------------------------
/**
* Custom validator definition.
*/
public class ProfileInputValidator extends UserInputValidator {
@Override
protected void buildRules() {
// Build a rule for each EditText, specifying the wanted format and error message
rulesMap.put(nameEditText, new Rule(Consts.REGEX_USERNAME, getString(R.string.profile_error_name)));
rulesMap.put(mobileNumberEditText, new Rule(Consts.REGEX_MOBILE, getString(R.string.profile_error_mobile)));
}
}
public ProfileInputValidator getInputValidator() {
if (inputValidator == null) {
inputValidator = new ProfileInputValidator();
}
return inputValidator;
}
}
/**
* Abstract class containing common validation functionalitites.
*/
public abstract class UserInputValidator {
/**
* Set containing all the EditTexts in error.
*/
protected Set<EditText> viewsInError;
/**
* All the validation rules associated to a field.
*/
protected Map<EditText, Rule> rulesMap;
public Set<EditText> getViewsInError() {
return viewsInError;
}
/**
* Default constructor, building validation rules.
*/
protected UserInputValidator() {
viewsInError = new HashSet<>();
rulesMap = new HashMap<>();
buildRules();
}
/**
* Build validation rules on the fields.
*/
protected abstract void buildRules();
/**
* Verify that the user input is valid.
*
* @return True if the input is correct.
*/
public boolean validateUserInput() {
for (Map.Entry<EditText, Rule> rule : rulesMap.entrySet()) {
EditText editText = rule.getKey();
Rule validationRule = rule.getValue();
boolean isInputValid = editText.getText() != null && editText.getText().toString().matches(validationRule.regExFormat);
ViewParent parent = editText.getParent();
TextInputLayout parentLayout = null;
String errorMessage;
if(parent instanceof TextInputLayout) {
parentLayout = (TextInputLayout) parent;
}
if(isInputValid) {
viewsInError.remove(editText);
errorMessage = null;
} else {
viewsInError.add(editText);
errorMessage = validationRule.errorMessage;
}
if(parentLayout != null) {
parentLayout.setError(errorMessage);
} else {
editText.setError(errorMessage);
}
}
return viewsInError.isEmpty();
}
/**
* A validation rule (wanted format, error message).
*/
protected static class Rule {
/**
* Wanted format expressed as a RegEx.
*/
final String regExFormat;
/**
* The error message shown if the input doesn't match.
*/
final String errorMessage;
public Rule(String regExFormat, String errorMessage) {
this.regExFormat = regExFormat;
this.errorMessage = errorMessage;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment