简体   繁体   English

如何在 Android 中使用 MVVM 数据绑定验证表单 ViewModel?

[英]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.我还有 Optional/Required 字段以及 Regex 验证。 I have implemented Android Data binding using MVVM and hence I can now not use saripar .我已经使用 MVVM 实现了 Android 数据绑定,因此我现在不能使用saripar

I tried Kubwa but i can not use duplicate annotations there.我试过Kubwa,但我不能在那里使用重复的注释。 I have also had a look at android-binding ,but I couldn't implement the library and also they don't support it anymore.我也看过android-binding ,但我无法实现该库,而且他们不再支持它。 Let me know if anyone can suggest better approach.如果有人可以建议更好的方法,请告诉我。

if by "duplicate annotations" you mean multiple of the same type (2 or more @Email validations) - then Kubwa will work.如果“重复注释”是指多个相同类型(2 个或更多@Email验证) - 那么 Kubwa 将起作用。 All the annotations have a .List sub annotation for such situations:对于这种情况,所有注释都有一个.List子注释:

@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 :)免责声明 - Kubwa 是我的图书馆 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MVVM(数据绑定)中的ViewModel与Android建筑组件中的ViewModel有何不同 - How ViewModel in MVVM (data binding) is differ from ViewModel in Android Architectural components 如何在MVVM Android中使用数据绑定处理ViewModel中的onClick或onTouch之类的事件 - How to handle onClick or onTouch like events in ViewModel with data binding in MVVM Android "如何在不绑定到 ViewModel (MVVM) 中的 UI 的情况下使用 android 导航?" - How to use android navigation without binding to UI in ViewModel (MVVM)? 如何使用 viewmodel mvvm 将自定义视图的数据保存在 Android 中? - How to keep Custom View's data in Android using viewmodel mvvm? Android MVVM-数据更改时更新ViewModel - Android MVVM - Update ViewModel when data changes 如何使用Android数据绑定库和MVVM开发应用程序? - How to develop application with Android Data Binding Library and MVVM? 如何在 Android(MVVM、数据绑定、Kotlin)中实现“记住我”function? - How to implement a “Remember me” function in Android(MVVM, Data Binding, Kotlin)? Android MVVM - 如何在 ViewModel 中引用 Activity - Android MVVM - How to reference Activity in ViewModel 如何在 Android MVVM ViewModel 中获取上下文 - How to get Context in Android MVVM ViewModel Android的ViewModel和MVVM - Android's ViewModel and MVVM
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM