lundi 8 mars 2021

Kotlin if and else statement is not working

fun registerdatabase(ctx: Context){
    val usernameValue2 = ctx.formParam(usernamefield)
    val nameValue2 = ctx.formParam(namefield)
    val familynameValue2 = ctx.formParam(familynamefield)
    val passwordValue2 = ctx.formParam(passwordfield)
    val emailfieldValue2 = ctx.formParam(emailfield)


    transaction {
        var error2: String? = null

        if(usernameValue2 == null || nameValue2 == null){
            error2 = "Please fill out all gaps"

        }else {
            val userEntity2 = registartedusers.select({registartedusers.username eq usernameValue2 }).firstOrNull()

            if (
                userEntity2 != null
            ) {
                error2 = "This username is already taken!"
            }
        }


        ctx.html(
            Page {
                if (error2 != null){
                    Head {
                        title{
                            +"Register Failed"
                        }
                    }
                    Body {
                        form {
                            method = FormMethod.post
                            action = ""

                            div {
                                + error2
                            }
                            div {
                                label {
                                    button {
                                        type = ButtonType.submit
                                        +"Back to register"
                                    }
                                }
                            }
                        }
                    }

                }else if (
                    usernameValue2 == null
                    || nameValue2 == null
                    || familynameValue2 == null
                    || passwordValue2 == null
                    || emailfieldValue2 == null
                ){
                    Head {
                        title {
                            +"Register Failed"
                        }
                    }
                    Body {
                        form {
                            method = FormMethod.post
                            action = ""

                            div {
                                +"Fill out all Gaps!"
                            }

                            div {
                                label {

                                    button {
                                        type = ButtonType.submit

                                        +"Back to Register"
                                    }
                                }
                            }
                        }
                    }

                }else{
                    transaction {
                        registartedusers.insert {
                            it[id] = UUID.randomUUID()
                            it[name] = nameValue2
                            it[familyname] = familynameValue2
                            it[email] = emailfieldValue2
                            it[password] = passwordValue2
                            it[username] = usernameValue2

                            Head {
                                title {
                                    +"Succesfully Registered"
                                }
                            }
                            Body {
                                div {
                                    +"Sucesfully Registered"
                                }
                            }
                        }
                    }
                }

            }
        )
    }



}

This is my Code and even if the Values are null it writes them down into the tables.Why? registartedusers is my database(i missytyped).

And the Code should check if the username is already in the table(And this works) but i can submit blank gaps and it would still write them into the database. The code doesnt check if the values are null...

I am working with Kotlin and xhtml.

Aucun commentaire:

Enregistrer un commentaire