Skip to content

User Input Validation

Validate user input is one of the hardship to code. This codebase feature easier way for doing it.

Rule

Main component for using validation is Rule. Rule is pre-defined validation to check does View return the desired value based on this sets of validation. Every time a Rule is added, add an error message for that validation is not fulfilled. Here are Rule that can be applie to View:

  • EditText, TextInputLayout

    • numberOnlyRule : Define that EditText must return number character, e.g:
      numberOnlyRule("This field should be number only")
      
    • minxMaxLengthRule : Define that View must return between min and max of length, e.g:
      minxMaxLenghtRule(5, 100, "Text must be more than 5 characters and less than 100 characters")
      
    • emailRule : Define that View must return email pattern text, e.g:
      emailRule("Inputted text is not an email")
      
    • notEmptyRule : Define that View can not return empty text, e.g:
      notEmptyRule("This field should not empty")
      
    • password6CharactersRule : Define that View must return password with minimal length of 6 characters, e.g:
      password6CharactersRule("Password must be more than 6 characters")
      
    • password8CharactersRule : Define that View must return password with minimal length of 8 characters, at least one capital character, at least one number, and at least one special character, e.g:
      password8CharactersRule("Password must contain one capital character, one number, one special character, and at least 8 characters of length")
      
    • alphaOnlyRule : Define that View must return alphabet only character, e.g:
      alphaOnlyRule("This field should not contain number")
      
    • sameValueRule : Define that View should return the same value as defined method.
      sameValueRule({ etPassword.text.toString() } ,"Inputted confirmed password is not the same")
      
  • RadioGroup, Spinner, CheckBox

    • checkedRule : Define that at least one component should be selected, e.g:
      checkedRule("This field should be selected")
      

Info

At the time, those are View that can be implemented with Rule. If Rule implemented to other component, it will throw Exception.

Implementation (using class or fragment)

Simply use validation by inherit DevFormActivity or DevFormFragment. Then override these two additional methods for validation:

  • onValidated - Operation when all the rules is fulfilled, e.g: enable button
  • onNotValidated - Operation when at least one rule is not fulfilled, e.g: disable button

Adding field

Then, start adding View to "form" using addFormField method:

addFormField(
   fieldName = "confirmPassword", // name of the field, for getting value purposes
   view = etConfirmPassword, // view to add to the form
   rules = listOf( // list of rules you want to implement to the view
         notEmptyRule("Password kudu diisi"),
         sameValueRule({ etPassword.text.toString() }, "Password harus sesuai")
         ),
   action = { error -> displayToast(this@FormActivity, error) } // custom action when view is not validated, nullable
)

Repeat this process as needed to your form.

Getting values

Lastly, get values from field by calling getFormValues method. It returns HashMap with field name as key and the field's value as the value of the HashMap:

 getFormValues()?.let {
                tvResultName.text = "Your Name: " + it["Name"]
                tvResultGender.text = "Your Gender: " + it["Gender"]
                tvResultEmail.text = "Your Email: " + it["Email"]
                tvResultLevel.text = "Your Level: " + it["Level"]
            } ?: logDebug { "SOMETHING ERROR" }
Even when the View is EditText, Spinner, CheckBox, or even RadioGroup... get the string value of the View by calling the field name defined when adding it to form.

Info

This method return hashMap when the form is validated, and null when the form is not validated.


Implementation (using Formulir class)

Input validation can be implemented without using the DevFormActivity or DevFormFragment, call this inside Activity or Fragment:

val form = Formulir { validated ->
    if (validated) {
        // Action when the form is validated
    } else {
        // Action when the form is not validated        
    }
}

Adding field

To add field to the Formulir, call this method:

form.addField(
    fieldName = "nameOtTheField", 
    view = viewOfTheField, 
    rules = listOf(ruleA,ruleB, ruleC), 
    action = null)

Getting values

For getting values from the field, call this method:

form.getValues()?.let {
    // it return hashMap, with fieldName as key, and View's value as value
}