Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mustafayigitt/validator
Notify type based validation for input fields.
https://github.com/mustafayigitt/validator
android android-validator input-validation kotlin validation validator
Last synced: 9 days ago
JSON representation
Notify type based validation for input fields.
- Host: GitHub
- URL: https://github.com/mustafayigitt/validator
- Owner: mustafayigitt
- License: apache-2.0
- Created: 2022-05-29T18:24:40.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-08-04T14:01:49.000Z (over 1 year ago)
- Last Synced: 2024-08-01T19:56:04.770Z (4 months ago)
- Topics: android, android-validator, input-validation, kotlin, validation, validator
- Language: Kotlin
- Homepage: https://blog.kotlin-academy.com/validator-rule-based-validation-library-in-android-2058e8d6c27
- Size: 125 KB
- Stars: 58
- Watchers: 4
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
- awesome-list - mustafayigitt/validator - Notify type based validation for input fields. (Kotlin)
README
Validator
## Import [![](https://jitpack.io/v/mustafayigitt/validator.svg)](https://jitpack.io/#mustafayigitt/validator)
- **project level**
````gradle
allprojects {
repositories {
// ...
maven { url 'https://jitpack.io' }
}
}
````- **module level**
````gradle
dependencies {
implementation 'com.github.mustafayigitt:validator:Tag'
}
````## How to use
````kotlin
// Create a validator with Builder pattern
val emailValidator = Validator.Builder()
.addRules(
ValidatableRule.Email("Enter valid mail address"),
ValidatableRule.Required("Email is required", NotifyType.ON_FOCUS_CHANGE),
ValidatableRule.Required("Email is required", NotifyType.ON_FORM_SUBMIT),
ValidatableRule.Email("Enter valid mail address", NotifyType.ON_FORM_SUBMIT),
)
.addCollector { validator ->
mBinding.txtInputEmail.editText?.doOnTextChanged { text, _, _, _ ->
validator.input = text.toString()
}
mBinding.txtInputEmail.editText?.setOnFocusChangeListener { _, isFocused ->
validator.isFocused = isFocused
}
}
.onValidate { isValid, errorMessage, notifyType ->
Log.d(
"Validator",
"notifyType: $notifyType, isValid: $isValid, errorMessage: $errorMessage"
)
mBinding.txtInputEmail.error = errorMessage
}
.build()
````### Or you can write a custom ValidatableRule
````kotlin
class PasswordConfirmRule(
override val errorMessage: String,
override val notifyType: NotifyType,
val originalInputGetter: () -> String
) : BaseValidatableRule(
errorMessage = errorMessage,
notifyType = notifyType,
rule = { it == originalInputGetter.invoke() }
)// Usage
val passwordConfirmValidator = Validator.Builder()
.addRules(
PasswordConfirmRule(
errorMessage = "Passwords does not match",
notifyType = NotifyType.ON_VALUE_CHANGE,
originalInputGetter = { mBinding.txtInputPassword.editText?.text.toString() },
)
)
...
````## License
```xml
Copyright 2022 mustafayigitt (Mustafa Yigit)Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
```