Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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.

Awesome Lists containing this project

README

        

Validator






License


Medium


Validate your inputs by notify types




## 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.0

Unless 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.
```