inmite / android-validation-komensky

A simple library for validating user input in forms using annotations.
Apache License 2.0
506 stars 78 forks source link

Cross field validation #5

Open sanrodari opened 10 years ago

sanrodari commented 10 years ago

I need to validate a password confirmation but on

public boolean validate(Annotation annotation, CharSequence input) { ...

I only have access to the value of the field itself and there is no way to access the password field.

I hacked the library in this nasty way:

@Custom(value = CustomValidatorInline.class, messageId = R.string.validation_password_no_coincidence)
private EditText editTextPasswordConfirmacion;
CustomValidatorInline.password = editTextPassword.getText().toString();
if(FormValidator.validate(this, new SimpleErrorPopupCallback(this))) { ...
public static class CustomValidatorInline extends BaseValidator<CharSequence> {

    private static String password;

    @Override
    public boolean validate(Annotation annotation, CharSequence input) {
        if (input == null) {
            return false;
        }
        else {
            String confirmation = input.toString();
            return confirmation.equals(password);
        }
    }

}
tomas-vondracek commented 10 years ago

I think @Joined annotation is what you need. This validation is missing in our description, so I will fix that.

Thanks!

ViksaaSkool commented 9 years ago

It's been a year and you haven't posted update. I think it's about time to do that. :)

nooone commented 8 years ago

+1

I can't figure out how to actually use @Joined. How to get the second input in the IValidator?

Okay, I got it. I didn't know it's a IValidator<String[]>:

public class NotEqualCrossValidator extends BaseValidator<String[]> {

    @Override
    public boolean validate(Annotation annotation, String[] input) {
        return !input[0].equals(input[1]);
    }

}