Commit 9cd14f6c authored by Javier Romero's avatar Javier Romero

WIP: Reimplement validation

parent c287ae71
Pipeline #30068800 failed with stages
in 6 minutes and 3 seconds
......@@ -2,7 +2,6 @@ package app.devlife.connect2sql.ui.connection.form
import android.content.Context
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.EditText
import app.devlife.connect2sql.db.model.connection.ConnectionInfo
......@@ -13,14 +12,14 @@ import app.devlife.connect2sql.ui.connection.form.section.MsSqlFormSection
import app.devlife.connect2sql.ui.connection.form.section.PostgresFormSection
import app.devlife.connect2sql.ui.connection.form.section.SshFormSection
import app.devlife.connect2sql.ui.widget.NotifyingScrollView
import app.devlife.connect2sql.util.ext.stringValue
import com.gitlab.connect2sql.R
import com.mobsandgeeks.saripaar.Rule
import com.mobsandgeeks.saripaar.Validator
import com.mobsandgeeks.saripaar.annotation.Required
open class Form(val context: Context, val view: View, private val driverType: DriverType) {
private val viewGroup: ViewGroup = view as ViewGroup
private val validator: Validator by lazy { Validator(this@Form) }
val actionBarContainer: ActionBarContainer = view.findViewById(R.id.form_actionbar)
val scrollView: NotifyingScrollView = view.findViewById(R.id.scroll_view)
......@@ -51,7 +50,7 @@ open class Form(val context: Context, val view: View, private val driverType: Dr
open fun compileConnectionInfo(): ConnectionInfo {
return ConnectionInfo(
-1,
"",
nameEditText.stringValue,
driverType,
"",
0,
......@@ -74,13 +73,35 @@ open class Form(val context: Context, val view: View, private val driverType: Dr
sections.forEach { it.populate(driverDefaults) }
}
open fun onPreValidate(validator: Validator) {
sections.forEach { it.onPreValidate(validator) }
fun validate(listener: Validator.ValidationListener) {
val firstFailingValidation = AggregatingValidationListener()
.also { aggregateListener ->
Validator(this@Form)
.also { it.validationListener = aggregateListener }
.validate()
sections.forEach { it.validate(aggregateListener) }
}
.failedValidations
.firstOrNull()
if (firstFailingValidation == null) {
listener.onValidationSucceeded()
} else {
listener.onValidationFailed(firstFailingValidation.first, firstFailingValidation.second)
}
}
fun validate(listener: Validator.ValidationListener) {
validator.validationListener = listener
onPreValidate(validator)
validator.validate()
private class AggregatingValidationListener : Validator.ValidationListener {
val failedValidations = mutableListOf<Pair<View?, Rule<*>?>>()
override fun onValidationFailed(failedView: View?, failedRule: Rule<*>?) {
failedValidations.add(Pair(failedView, failedRule))
}
override fun onValidationSucceeded() {
// ignored
}
}
}
......@@ -9,7 +9,11 @@ interface FormSection {
fun populate(connectionInfo: ConnectionInfo)
fun populate(driverDefaults: DriverDefaults)
fun populate(driverDefaults: DriverDefaults) {}
fun onPreValidate(validator: Validator) {}
fun validate(listener: Validator.ValidationListener) {
Validator(this)
.also { it.validationListener = listener }
.validate()
}
}
\ No newline at end of file
package app.devlife.connect2sql.ui.connection.form.section
import android.content.Context
import android.text.TextUtils
import android.view.View
import android.widget.EditText
import android.widget.Switch
import app.devlife.connect2sql.db.model.connection.ConnectionInfo
import app.devlife.connect2sql.sql.driver.DriverDefaults
import com.gitlab.connect2sql.R
import com.mobsandgeeks.saripaar.Rule
import com.mobsandgeeks.saripaar.Validator
import com.mobsandgeeks.saripaar.annotation.Required
class PostgresFormSection(
private val context: Context,
private val view: View,
@Required(order = 9, messageResId = R.string.form_error_database_required)
private val databaseEditTextView: EditText
) :
FormSection {
......@@ -34,18 +32,4 @@ class PostgresFormSection(
useSslSwitch.isChecked = connectionInfo.options[ConnectionInfo.OPTION_USE_SSL]?.toBoolean() ?: false
trustCertSwitch.isChecked = connectionInfo.options[ConnectionInfo.OPTION_TRUST_CERT]?.toBoolean() ?: false
}
override fun populate(driverDefaults: DriverDefaults) {
// ignore
}
override fun onPreValidate(validator: Validator) {
val errorMessage = context.resources.getString(R.string.form_error_database_required)
validator.put(databaseEditTextView,
object : Rule<EditText>(errorMessage) {
override fun isValid(databaseEditTextView: EditText): Boolean {
return !TextUtils.isEmpty(databaseEditTextView.text)
}
})
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment