Home » Android » android – Saripaar formvalidation not working second time in kotlin

android – Saripaar formvalidation not working second time in kotlin

Posted by: admin May 14, 2020 Leave a comment


I am using saripaar for form validation. I have some edittext in fragmentA, on validation success , view will be switched to fragmentB.
Butterknife and saripaar annotation in fragment.

lateinit var nameEditText: EditText

Saripaar initialization:

 val validator = Validator(this)

To validate the fields:


Validation is working fine for the first time. When come back from fragmentB to fragmentA, then validation is not working, it will directly call onvalidationsuccess.

On onValidationSucceeded , I am using following function to switch to fragmentB.

fun openFragment(fragment: Fragment) {
    val ft = activity.supportFragmentManager.beginTransaction()
    ft.replace(R.id.container, fragment)

This problem only appears in kotlin but not in java.

How to&Answers:

I solved the issue, I have all the saripaar initialization in basefragment, I have the following code in basefragment with null check for validator instance:

 validator = Validator(this) 

As I removed the null check part, now its working fine.

validator = Validator(this)