ragunathjawahar / android-saripaar

UI form validation library for Android
Apache License 2.0
3.22k stars 460 forks source link

How do I use it in Kotlin? #198

Closed KimGenius closed 5 years ago

KimGenius commented 7 years ago

@NotEmpty @Email private EditText emailEditText;

@Password(min = 6, scheme = Password.Scheme.ALPHA_NUMERIC_MIXED_CASE_SYMBOLS) private EditText passwordEditText;

@ConfirmPassword private EditText confirmPasswordEditText;

@Checked(message = "You must agree to the terms.") private CheckBox iAgreeCheckBox;

Kotlin gets the xml id and uses view, how do you handle this part?

DamonChen117 commented 6 years ago

+1

tapanrgohil commented 6 years ago

any solution?

codenameDuchess commented 6 years ago

Using Butterknife and Kotlin I am able to compile:

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

    @NotEmpty
    @Password(min = 8, scheme = Password.Scheme.ALPHA_NUMERIC_MIXED_CASE)
    @BindView(R.id.passwordTextField)
    lateinit var passwordTextField: EditText

    private lateinit var validator: Validator

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.login)
        presenter?.onCreate(intent.extras)

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

However, when I call validator.validate() I receive an exception on Validator.java line 549 (Annotation[] annotations = field.getAnnotations();).

I can see in debug that it does have the reference to the correct field that I added the Email annotation to, and then in createAnnotation I see that it does recognize the email rule correctly, but after that the error thrown is:

{AnnotationFactory init 110} Method threw 'java.lang.NullPointerException' exception. Cannot evaluate libcore.reflect.AnnotationFactory.toString()

Any help you can offer is appreciated.

ragunathjawahar commented 6 years ago

Kotlin is not supported yet, would appreciate if someone sends a PR.