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

Questions:

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.

@NotEmpty
@BindView(R.id.nameEditText)
lateinit var nameEditText: EditText

Saripaar initialization:

 val validator = Validator(this)
 validator.setValidationListener(this)

To validate the fields:

validator.validate()

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)
    ft.addToBackStack(null)
    ft.commitAllowingStateLoss()
}

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:

if(validator==null){  
 validator = Validator(this) 
 validator.setValidationListener(this) 
}

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

validator = Validator(this) 
validator.setValidationListener(this)