I just started using android saripaar library for a client's app. I wanted to add a custom validation for a field. However, there doesn't seem to be a way to create a custom annotation. I have to manually put in rule in the validator.
How do I create a custom annotation for the same?
(Disclosure: I'm the author)
Saripaar v2 allows you to define custom annotations.
Here's how you do it.
Step 1 Define your custom annotation as follows. Make sure you have a
RUNTIME
retention policy and your annotation must be targeted towardsFIELD
element types. Themessage
andmessageResId
attributes are mandatory, so watch the names and the types.Step 2 Define your rule by extending the
AnnotationRule
class.Step 3 Register your rule.
Simple as that. Take a look at the source code if you want to. Saripaar v2 is now available on Maven Central.