androidvalidationmvvmdata-bindingformvalidation.io

How to validate form ViewModel with MVVM Data binding in Android?


I am trying to validate a form with mixed data type inputs from user. I also have Optional/Required field along with Regex validations. I have implemented Android Data binding using MVVM and hence I can now not use saripar.

I tried Kubwa but i can not use duplicate annotations there. I have also had a look at android-binding,but I couldn't implement the library and also they don't support it anymore. Let me know if anyone can suggest better approach.


Solution

  • if by "duplicate annotations" you mean multiple of the same type (2 or more @Email validations) - then Kubwa will work. All the annotations have a .List sub annotation for such situations:

    @NotNull.List({
        @NotNull(name = "streetError", errorMessage = R.string.streetError),
        @NotNull(name = "cityError", errorMessage = R.string.cityError),
        @NotNull(name = "countryError", errorMessage = R.string.countryError)
    })
    

    Disclaimer - Kubwa is my library :)