Skip to content

Using With Annotations

Wellington Costa edited this page Apr 26, 2018 · 5 revisions

Step 1 - Annotate your fields with Convalida Annotations:

@RequiredValidation(errorMessage = R.string.field_required)
EditText nameField;

@LengthValidation(errorMessage = R.string.min_3_characters, min = 3)
EditText nickNameField;

@OnlyNumberValidation(errorMessage = R.string.only_numbers)
EditText ageField;

@EmailValidation(errorMessage = R.string.invalid_email)
EditText emailField;

@ConfirmEmailValidation(errorMessage = R.string.emails_not_match)
EditText confirmEmailField;

@PatternValidation(errorMessage = R.string.invalid_phone, pattern = PHONE_PATTERN)
EditText phoneField;

@CpfValidation(errorMessage = R.string.cpf_not_valid)
EditText cpfField;

@BetweenValidation.Start(key = 1, errorMessage = R.string.initial_period_not_valid)
EditText initialPeriodField;

@BetweenValidation.End(key = 1, errorMessage = R.string.final_period_not_valid)
EditText finalPeriodField;

@PasswordValidation(min = 3, errorMessage = R.string.invalid_password)
EditText passwordField;

@ConfirmPasswordValidation(errorMessage = R.string.passwords_not_match)
EditText confirmPasswordField;

Note: The key parameter in the @BetweenValidation.Start and @BetweenValidation.End annotations serves to identify the field pairs.

Step 2 - Map the buttons with the validation actions:

@ValidateOnClick
Button validateButton;

@ClearValidationsOnClick
Button clearValidations;

Note: The use of the @ClearValidationsOnClick annotation is not mandatory.

Step 3 - Map the methods to handle the validation results:

@OnValidationSuccess
public void onValidationSuccess() {
    Toast.makeText("Yay!", Toast.LENGTH_LONG).show();
}

@OnValidationError
public void onValidationError() {
    Toast.makeText("Something is wrong :(", Toast.LENGTH_LONG).show();
}

Note: Only the method annotated with @OnValidationSuccess is required.

Step 4 - Initialize the generated class:

Note: The generate class is localized at the same package that the source class and its name is the same name of the source class with suffix FieldsValidation.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sample);
    SampleActivityFieldsValidation.init(this);
}
Clone this wiki locally