Skip to content

Commit

Permalink
FEAT: implement proper validation
Browse files Browse the repository at this point in the history
  • Loading branch information
Zdeněk Balák committed May 2, 2019
1 parent 1d2c929 commit 4009c95
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import cz.qase.android.formbuilderlibrary.element.generic.CheckboxCallback
import cz.qase.android.formbuilderlibrary.element.generic.ValueCallback
import cz.qase.android.formbuilderlibrary.validator.MaxLengthFormValidator
import cz.qase.android.formbuilderlibrary.validator.NotBlankFormValidator
import cz.qase.android.formbuilderlibrary.validator.NotInPastValidator
import kotlinx.android.synthetic.main.activity_main.*
import org.joda.time.DateTime
import java.text.SimpleDateFormat
Expand Down Expand Up @@ -96,7 +97,7 @@ class MainActivity : AppCompatActivity() {
addElement(LabelSwitchElement("LabelSwitchElement label", true, showToastCheckboxCallback), true)
addElement(LabelSpinnerElement("LabelSpinnerElement label", "Option one", stringValues, showToastStringValueCallback), true)
addElement(LabelCheckboxElement("LabelCheckboxElement label", true, showToastCheckboxCallback), true)
addElement(LabelDateTimeElement("Date picker", "Vyberte datum...", supportFragmentManager, valueChangeListener = showToastDateTimeValueCallback))
addElement(LabelDateTimeElement("Date picker", "Vyberte datum...", supportFragmentManager, showToastDateTimeValueCallback, formValidators = arrayListOf(NotInPastValidator("Date cannot be in past"))))
addSpace()


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,16 @@ class Form(val context: Context,

@Throws(ValidationException::class)
fun validate() {
var validationException: ValidationException? = null
for (formElement in elements) {
formElement.validate()
try {
formElement.validate()
} catch (e: ValidationException) {
validationException = e
}
}
if(validationException!=null){
throw validationException
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.text.Editable
import android.text.TextWatcher
import android.view.View
import cz.qase.android.formbuilderlibrary.FormStyleBundle
import cz.qase.android.formbuilderlibrary.ValidationException
import cz.qase.android.formbuilderlibrary.common.setBackgroundColorResourceId
import cz.qase.android.formbuilderlibrary.common.setTextColorResourceId
import cz.qase.android.formbuilderlibrary.element.generic.FormElementValidatable
Expand Down Expand Up @@ -52,10 +53,8 @@ open class EditTextElement(
editText?.addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(s: Editable) {
text = s.toString()
validate()
if(!invalid) {
valueChangeListener?.callback(s.toString())
}
positiveValidation()
valueChangeListener?.callback(s.toString())
}

override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
Expand All @@ -68,14 +67,21 @@ open class EditTextElement(
return textInputLayout!!
}

private fun positiveValidation() {
try {
super.validate()
textInputLayout?.error = null
} catch (e: ValidationException) {
}
}

override fun validate() {
super.validate()
if (textInputLayout != null) {
if (invalid) {
textInputLayout?.error = invalidMessage
} else {
textInputLayout?.error = null
}
try {
super.validate()
textInputLayout?.error = null
} catch (e: ValidationException) {
textInputLayout?.error = e.message
throw e
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import android.view.ViewGroup
import android.widget.TextView
import cz.qase.android.formbuilderlibrary.FormStyleBundle
import cz.qase.android.formbuilderlibrary.R
import cz.qase.android.formbuilderlibrary.ValidationException
import cz.qase.android.formbuilderlibrary.common.setBackgroundColorResourceId
import cz.qase.android.formbuilderlibrary.common.setTextColorResourceId
import cz.qase.android.formbuilderlibrary.element.generic.FormElementValidatable
Expand Down Expand Up @@ -58,6 +59,7 @@ class LabelDateTimeElement(private val label: String,
view.setBackgroundColorResourceId(context, formStyleBundle.secondaryBackgroundColor)
}
MotionEvent.ACTION_DOWN -> {
textView.requestFocus()
view.setBackgroundColorResourceId(context, formStyleBundle.primaryBackgroundColor)
}
}
Expand All @@ -78,6 +80,7 @@ class LabelDateTimeElement(private val label: String,
val dateTime = DateTime(newDate)
value = dateTime
textView.text = sdf.format(newDate)
positiveValidation()
valueChangeListener.callback(dateTime)
}
textView.setOnClickListener {
Expand All @@ -102,6 +105,25 @@ class LabelDateTimeElement(private val label: String,
textView?.text = text
}

private fun positiveValidation() {
try {
super.validate()
textView?.error = null
} catch (e: ValidationException) {
}
}

override fun validate() {
try {
super.validate()
textView?.error = null
} catch (e: ValidationException) {
textView?.text = e.message
textView?.error = e.message
throw e
}
}

private fun prepareLabel(inflater: LayoutInflater, context: Context, formStyleBundle: FormStyleBundle, root: ViewGroup): TextView {
val headerView = inflater.inflate(headerComponent, root, false) as TextView
headerView.setTextColorResourceId(context, formStyleBundle.primaryTextColor)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import android.view.ViewGroup
import android.widget.TextView
import cz.qase.android.formbuilderlibrary.FormStyleBundle
import cz.qase.android.formbuilderlibrary.R
import cz.qase.android.formbuilderlibrary.ValidationException
import cz.qase.android.formbuilderlibrary.common.setBackgroundColorResourceId
import cz.qase.android.formbuilderlibrary.common.setTextColorResourceId
import cz.qase.android.formbuilderlibrary.element.generic.FormElementValidatable
Expand Down Expand Up @@ -40,7 +41,8 @@ class LabelInputElement(private val label: String,
val view = inflater.inflate(groupComponent, null) as ViewGroup
val headerView = prepareLabel(inflater, context, this.formStyleBundle
?: formStyleBundle, view)
val inputView = prepareText(inflater, context, this.formStyleBundle ?: formStyleBundle, view)
val inputView = prepareText(inflater, context, this.formStyleBundle
?: formStyleBundle, view)
view.setBackgroundColorResourceId(context, formStyleBundle.secondaryBackgroundColor)
view.addView(headerView)
view.addView(inputView)
Expand All @@ -59,9 +61,8 @@ class LabelInputElement(private val label: String,
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
value = s.toString()
positiveValidation()
if (!invalid) {
valueChangeListener.callback(s.toString())
}
valueChangeListener.callback(s.toString())

}

override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
Expand All @@ -83,19 +84,21 @@ class LabelInputElement(private val label: String,
textInputEditText?.setText(text)
}

private fun positiveValidation(){
super.validate()
if (!invalid) {
private fun positiveValidation() {
try {
super.validate()
textInputLayout?.error = null
} catch (e: ValidationException) {
}
}

override fun validate() {
super.validate()
if (invalid) {
textInputLayout?.error = invalidMessage
} else {
try {
super.validate()
textInputLayout?.error = null
} catch (e: ValidationException) {
textInputLayout?.error = e.message
throw e
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,16 @@ package cz.qase.android.formbuilderlibrary.element.generic
import cz.qase.android.formbuilderlibrary.ValidationException
import cz.qase.android.formbuilderlibrary.validator.FormValidator

abstract class FormElementValidatable<T>(val formValidators: MutableList<FormValidator<T>> = ArrayList(),
var invalid: Boolean = false,
var invalidMessage: String? = null) : FormElement<T>() {
abstract class FormElementValidatable<T>(val formValidators: MutableList<FormValidator<T>> = ArrayList()) : FormElement<T>() {

fun addValidator(formValidator: FormValidator<T>) {
formValidators.add(formValidator)
}

@Throws(ValidationException::class)
override fun validate() {
invalid = false
invalidMessage = null
for (formValidator in formValidators) {
try {
formValidator.validate(getVal())
} catch (e: ValidationException) {
invalid = true
invalidMessage = e.message
}
formValidator.validate(getVal())
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package cz.qase.android.formbuilderlibrary.validator

import cz.qase.android.formbuilderlibrary.ValidationException
import org.joda.time.DateTime

/**
* MaxLengthFormValidator
*
* Check if length of form value is bigger than specified value
*/
class NotInFutureValidator(
private val errorMsg: String
) : FormValidator<DateTime> {
override fun validate(value: DateTime?) {
if (value != null && value.isAfterNow) {
throw ValidationException(errorMsg)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package cz.qase.android.formbuilderlibrary.validator

import cz.qase.android.formbuilderlibrary.ValidationException
import org.joda.time.DateTime

/**
* MaxLengthFormValidator
*
* Check if length of form value is bigger than specified value
*/
class NotInPastValidator(
private val errorMsg: String
) : FormValidator<DateTime> {
override fun validate(value: DateTime?) {
if (value != null && value.isBeforeNow) {
throw ValidationException(errorMsg)
}
}
}

0 comments on commit 4009c95

Please sign in to comment.