Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom Validator lacks annotation for constructor args #8

Open
almozavr opened this issue Mar 20, 2014 · 1 comment
Open

Custom Validator lacks annotation for constructor args #8

almozavr opened this issue Mar 20, 2014 · 1 comment

Comments

@almozavr
Copy link

Scope

E.g.

public class BirthdayValidator extends BaseValidator<CharSequence> {

    private String pattern;

    public BirthdayValidator(String pattern) {
        this.pattern = pattern;
    }

    @Override
    public boolean validate(Annotation annotation, CharSequence input) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, cal.get(Calendar.YEAR) - 18);
        long acceptedBirthday = cal.getTimeInMillis();

        long birthday;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
        try {
            birthday = simpleDateFormat.parse(input.toString()).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
            return false;
        }
        return birthday <= acceptedBirthday;
    }
}

As I see from ValidatorFactory all validators are created with no-args constructor and this is pretty sad for such a nice library

public static IValidator getValidator(Annotation annotation) throws IllegalAccessException, InstantiationException {
        if (annotation == null) {
            return null;
        }

        final Class<? extends IValidator> clazz = sValidators.get(annotation.annotationType());

        IValidator validator = null;
        if (clazz != null) {
            validator = sCachedValidatorInstances.get(clazz);
            if (validator == null) {
                validator = clazz.newInstance();
                sCachedValidatorInstances.put(clazz, validator);
            }
        }
        return validator;
    }

or even only

@ValidatorFor({Joined.class, Custom.class})
public class CustomValidator extends BaseValidator<Object> {

    @Override
    public boolean validate(Annotation annotation, Object input) {
        if (annotation instanceof Joined) {
            try {
                IValidator validator = ((Joined) annotation).validator().newInstance();
                return validator.validate(annotation, input);
            } catch (InstantiationException e) {
                throw new FormsValidationException(e);
            } catch (IllegalAccessException e) {
                throw new FormsValidationException(e);
            }
        } else if (annotation instanceof Custom) {
            try {
                IValidator validator = ((Custom) annotation).value().newInstance();
                return validator.validate(annotation, input);
            } catch (InstantiationException e) {
                throw new FormsValidationException(e);
            } catch (IllegalAccessException e) {
                throw new FormsValidationException(e);
            }
        } else {
            throw new FormsValidationException("unknown annotation " + annotation);
        }
    }

}

What is wanted

@Custom(value = BirthdayValidator.class, args = {"MM/dd/yyyy hh:mm:ss aa"}, messageId = R.string.error_birthday_required)
@douglasjunior
Copy link
Contributor

I created an extension instead of using the @Custom.

Example https://gist.github.com/douglasjunior/5bf82f11f5f006a4b5b9

Works for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants