Field validator for Android EditText's which uses both old school programmatic way to set up validated views or annotation processing to generate boilerplate code for you.
- Validate inputs based on target type;
- Supports multiple actions when fields would be validated;
- Custom handling error conditions by field
- Pre-defined default error messages translated into English;
NAME
EMAIL
PHONE
PASSWORD
CREDIT_CARD
ON_CLICK
ON_FOCUS
ON_FOCUS_MISS
ON_TEXT_CHANGE
AFTER_TEXT_CHANGE
BEFORE_TEXT_CHANGE
Validation conditions use default patterns from the android library and can be simply replaced by setuping your custom validating class
in progress
public class LoginExampleFragment extends Fragment {
private EditText mEmail;
private EditText mPassword;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_login, container, false);
bind(root);
FieldsHandleManager.getInstance(this)
.target(mEmail, EMAIL)
.target(mPassword, PASSWORD, ON_TEXT_CHANGE, ON_FOCUS_MISS)
.setOnHandleResultListener(result -> {
if (!result.isSuccess()) {
showError(result.getMessage());
}
});
return root;
}
}
You can simply setup field handler using TargetHandler builder
- Supports adding your custom particular errors using add(int code, @StringRes int stringResId, ParticularHandler handler) method.
- Supports removing already setted up errors using remove(int code) method.
- Supports references to string resources ids
public class RegistrationFragment extends Fragment {
public static final int PASSWORD_VERIFICATION = 1;
public static final int PASSWORDS_DONT_MATCH = 2;
...
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
...
FieldsHandleManager.getInstance(this)
.target(mPassword, PASSWORD)
.target(mPasswordVerification, PASSWORD_VERIFICATION, TargetHandler.getBuilder()
.add(PASSWORDS_DONT_MATCH, "Passwords don't match", target -> target.equals(mPassword.getText().toString()))
.remove(TEXT_CONTAINS_UPPER_CASE)
.build());
...
}
- Setup validated field by using
@HandleField
annoation (Default action isON_FOCUS_MISS
) - Setup method that will receive the result of validating the fields using
@OnFieldHandleResult
annonation. - Finally Bind class where fields should be proccessed using
UniversalHandleManager.bind(class)
or bind target activity or fragment where your fields places separetly useUniversalHandleManager.bindTarget(target)
method and for destination class where your recieving method places useUniversalHandleManager.bindDestination(destination)
method.
public class RegistrationExampleFragment extends Fragment {
@HandleField(EMAIL)
EditText mEmail;
@HandleField(value = PASSWORD, action = ON_TEXT_CHANGE)
EditText mPassword;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_registration, container, false);
bind(root);
UniversalHandleManager.bind(this);
return root;
}
@OnFieldHandleResult("RegistrationExampleFragment")
public void onFieldsHandleResult(int fieldType, boolean isSuccess) {
if (!isSuccess) {
switch (fieldType) {
case EMAIL: {
showError("Email not correct");
break;
}
case PASSWORD: {
showError("Password can't be empty");
break;
}
}
}
}