EasyValidation
  • Introduction
  • Including in your project
  • Usage
    • Validator Class
    • Extension Methods
    • Multiple Validation Checks
    • Collection Extensions
    • Built-in Rules
    • Create Custom Rules
  • Modules
    • Toasts
Powered by GitBook
On this page
  1. Usage

Create Custom Rules

This section describes how to create your own custom rules.

PreviousBuilt-in RulesNextToasts

Last updated 6 years ago

Along with , 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()
30+ built-in rules