📜 ⬆️ ⬇️

Android Input Validators

Validation
Not so long ago, I needed to add validators for input fields (EditText) to an android application, but nothing suitable was found on the network: I had to write my own. Now that everything is ready, you can distribute the results to everyone under the Apache license and tell you a little about how everything works. Those who are interested I ask under the cat.

Since everything was written from scratch, it was possible to make the architecture by itself, so the following scheme was chosen:

For convenience, I made a couple of customizations:

The validators and checkers have base abstract classes, by implementing which you can get validators for your controls and your own special checks.

Now there are checks:

In the code, it all looks something like this:
//   ValidationSummary summaryValidator_ = new ValidationSummary(getString(R.string.common_error_message), headerErrorMessage); //      EditTextValidator firstnameValidator = new EditTextValidator(); //  EditText  ,     (  ,     ) firstnameValidator.setViewToValidate(firstnameEdit, ValidationMode.Manual); // TextView       ,       firstnameValidator.setExternalErrorView( (TextView) findViewById(R.id.firstname_error_text)); //     firstnameValidator.addConditionChecker(new NotEmptyChecker( getString(R.string.name_required_error_message) )); //   firstnameValidator.addConditionChecker(new LengthChecker(1, 50, getString(R.string.firstname_error_message) )); //    summaryValidator_.addValidator(firstnameValidator); ... //     EditTextValidator confirmPasswordValidator = new EditTextValidator(); confirmPasswordValidator.setViewToValidate(confirmPasswordEdit, ValidationMode.Manual); confirmPasswordValidator.setExternalErrorView( (TextView) findViewById(R.id.confirm_password_error_text)); //    ,    - passwordEdit confirmPasswordValidator.addConditionChecker(new TextMatchChecker(getString(R.string.confirm_password_error_message), passwordEdit)); summaryValidator_.addValidator(confirmPasswordValidator); 

')
You can initiate a check of all validators in a group by calling:
 summaryValidator_.performCheck(); 


All those who are interested in validators, I invite you to take a project from the GitHub of Rus Wizards , within which this code was written.
And, of course, any suggestions, suggestions, advice are welcome!

Source: https://habr.com/ru/post/136267/


All Articles