Create Custom Rules
This section describes how to create your own custom rules.
Along with 30+ built-in rules, you can create your own custom rules very easily.
First step is you have to create your Rule
class by extending BaseRule
or any other existing rule like this.
class HelloRule : BaseRule
{
// add your validation logic in this method
override fun validate(text: String) : Boolean
{
// Apply your validation rule logic here
return text.contains("hello")
}
// Add your invalid check message here
override fun getErrorMessage() : String
{
return "Text should contain 'hello' keyword"
}
}
You can use this rule using Validator.addRule()
method like this:
var myEditText = findViewById<EditText>(R.id.myEditText)
var myEditText.validator()
.addRule(HelloRule())
.addErrorCallback {
// In case of invalid, this method will be called.
// The 'it' will be "Text should contain 'hello' keyword" message.
myEditText.error = it
}
.check()
Last updated