The library contains a set of base classes for validation.
- Validator - Interface for field Validation class
- PhoneValidator - Interface for advanced phone validation
- MatchValidator - Interface for validation two fields with matching
- DateValidator - Interface for validation
- ValidationResponse - Interface for encapsulation information about results of validation
- MatchValidationResponse - Interface for encapsulating result of validation of more then 1 fields
- NameValidator
- EmailValidator
- PasswordValidator
- MatchPasswordValidator
- MatchValidationResponseImpl
- PhoneValidatorImpl
- DateValidatorImpl
- ValidationResponseImpl
by gradle :
dependencies {
implementation 'com.cleveroad.bootstrap:kotlin-validators:2.0.0'
}
class ValidatorActivity : AppCompatActivity() {
private var nameValidator: Validator? = null
private var emailValidator: Validator? = null
private var nameWatcher: SimpleTextWatcher? = null
private var emailWatcher: SimpleTextWatcher? = null
companion object {
fun start(context: Context) = context.startActivity(Intent(context, ValidatorActivity::class.java))
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_validator)
initValidators()
nameWatcher = etName.addSimpleTextChangedListener { text ->
nameValidator?.validate(text)?.let { showTextInputLayoutError(tilName, it) }
}
emailWatcher = etEmail.addSimpleTextChangedListener { _ -> hideError(tilEmail) }
etEmail.addFocusChangedListener { hasFocus ->
if (!hasFocus) {
emailValidator?.validate(etEmail.text.toString())
?.let { showTextInputLayoutError(tilEmail, it) }
}
}
bValidateName.setOnClickListener {
nameValidator?.validate(etName.text.toString())?.let { showValidateResponse(it) }
}
bValidateEmail.setOnClickListener {
emailValidator?.validate(etEmail.text.toString())?.let { showValidateResponse(it) }
}
}
private fun showTextInputLayoutError(til: TextInputLayout, validate: ValidationResponse) {
til.error = if (validate.isValid) "" else validate.errorMessage ?: getString(R.string.error)
}
private fun hideError(til: TextInputLayout) {
til.error = ""
}
override fun onStop() {
etName.removeTextChangedListener(nameWatcher)
etEmail.removeTextChangedListener(emailWatcher)
etEmail.onFocusChangeListener = null
super.onStop()
}
private fun initValidators() {
nameValidator = ValidatorsFactory.getNameValidator(this)
emailValidator = EmailValidator.Builder(this)
.emptyError("Email is empty")
.invalidError("Email is invalid")
.build()
}
private fun showValidateResponse(validate: ValidationResponse) {
if (validate.isValid) showMessage() else showMessage(validate.errorMessage
?: getString(R.string.error))
}
private fun showMessage(message: String = getString(R.string.correct)) =
Snackbar.make(findViewById(android.R.id.content), message, Snackbar.LENGTH_SHORT).show()
}
If you have any questions, issues or propositions, please create a new issue in this repository.
If you want to hire us, send an email to sales@cleveroad.com or fill the form on contact page
Follow us:
The MIT License (MIT)
Copyright (c) 2016 Cleveroad Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.