Convalida
Convalida - (Italian for "validation")
Convalida is a simple, lightweight and powerful field validation library for Android.
Why Convalida?
- Annotation-based;
- Compile-time;
- Compatible with other annotation-based libraries and frameworks such as ButterKnife, AndroidAnnotations, etc;
- Supports Material Design Error Patterns;
Quick Start
Step 1 - Annotate your fields with Convalida Annotations:
private static final String PHONE_PATTERN = "^\\([1-9]{
2
}
\\)?([0-9]{
9
}
)$"; @NotEmptyValidation(R.string.field_required) @LengthValidation(min = 3, errorMessage = R.string.min_3_characters) TextInputLayout nameLayout; @OnlyNumberValidation(R.string.only_numbers) TextInputLayout ageLayout; @EmailValidation(R.string.invalid_email) TextInputLayout emailLayout; @PatternValidation(errorMessage = R.string.invalid_phone, pattern = PHONE_PATTERN) TextInputLayout phoneLayout; @PasswordValidation(min = 3, pattern = LOWER_UPPER_CASE_NUMERIC, errorMessage = R.string.invalid_password) TextInputLayout passwordLayout; @ConfirmPasswordValidation(R.string.passwords_not_match) TextInputLayout confirmPasswordLayout;
Step 2 - Initialize Convalida:
private ConvalidaValidator validator; @Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sample);
validator = Convalida.init(this);
}
Step 3 - Run the validations and get the result:
public void validateFields() {
boolean isValid = validator.validateFields();
String message = isValid ? "Yay!" : "Something is wrong :(";
Snackbar.make(linearLayout, message, Snackbar.LENGTH_LONG).show();
}
Step 4 - If you want to clear the validations:
public void clearFields() {
validator.clearValidations();
}
Remember: You must initialize the views (e.g ButterKnife) before apply the validations.
Download
Step 1 - Add the JitPack repository to your root build.gradle file:
allprojects {
repositories {
...
maven {
url 'https://jitpack.io'
}
}
}
Step 2 - Add the dependencies:
dependencies {
compile 'com.github.WellingtonCosta.convalida:convalida:1.0.5'
annotationProcessor 'com.github.WellingtonCosta.convalida:convalida-compiler:1.0.5'
}
License
Copyright 2017 Wellington Costa Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.