Validaciones para la entrada de datos de la aplicación

 

Aquí te dejamos el código utilizado en esta lección:

Module Accounts {
    Bool validateSignUp (SignUp signUpExp) {
        if (signUpExp.username.trim() == "" || signUpExp.username.length() < 3) {
            broker.ui.showAlert('Oops', 'At least 3 characters are required for your username.')
            return false
        }

        if (signUpExp.password == null || signUpExp.password.length() < 6) {
            broker.ui.showAlert('Oops', 'At least 6 characters are required for your password.')
            return false
        }

        if (signUpExp.password != signUpExp.repeatPassword) {
            broker.ui.showAlert('Oops', 'The passwords you have entered are not the same.')
            return false
        }

        Filter userNameFilter = Filter(modelName: "User")
        userNameFilter.equalTo("name", signUpExp.username)
        Array<User> existingUsers = broker.cloudPersistence.getWithFilter(userNameFilter)

        if (existingUsers.size() > 0) {
            broker.ui.showAlert('Oops', 'The username already exists, please try another one.')
            return false
        }

        return true
    }
}

RuleContext SignUpContext {
    Rule createAccount {
        SignUp signUpExp = broker.ui.getDataSource()

        if (Accounts.validateSignUp(signUpExp)) {
            User newUser = User()
            newUser.name = signUpExp.username.trim()
            newUser.password = signUpExp.password
            broker.cloudPersistence.save(newUser)
            broker.ui.showAlert("Congratulations!", "Your account has been created successfuly, please login now.")
            broker.ui.pop()
        }
    }
}