From 0bd7d7f98bbb9a648771ddd6047d33134b64c40d Mon Sep 17 00:00:00 2001 From: molexx Date: Sun, 12 Nov 2017 22:35:45 +0000 Subject: [PATCH 1/2] Add attribute showErrorMessage to allow validation whilst not showing the message --- README.md | 9 +++++++ .../validation/binding/ConfigBindings.java | 25 +++++++++++++++++++ .../validation/util/EditTextHandler.java | 15 ++++++----- library/src/main/res/values/ids.xml | 1 + 4 files changed, 44 insertions(+), 6 deletions(-) create mode 100644 library/src/main/java/br/com/ilhasoft/support/validation/binding/ConfigBindings.java diff --git a/README.md b/README.md index 2f98d39..f5d188f 100644 --- a/README.md +++ b/README.md @@ -224,6 +224,15 @@ By default, the library prompts error messages and doens't dismiss the error aut app:validateDateAutoDismiss="@{true}" /> ``` +### Hiding validation messages ### + +If you want to check if fields are valid but not have the EditText's error message set, add an attribute: +``` +app:showErrorMessage='@{false}' +``` +This is useful if you're validating as the user types to enable a button - showing an error after the user has only typed one character is not an ideal user experience. You can use the databinding expression to show the message according to your requirements, such as after onFocusChanged when the user navigates out of the field. + + ## License ## Copyright 2017-present Ilhasoft diff --git a/library/src/main/java/br/com/ilhasoft/support/validation/binding/ConfigBindings.java b/library/src/main/java/br/com/ilhasoft/support/validation/binding/ConfigBindings.java new file mode 100644 index 0000000..2c80178 --- /dev/null +++ b/library/src/main/java/br/com/ilhasoft/support/validation/binding/ConfigBindings.java @@ -0,0 +1,25 @@ +package br.com.ilhasoft.support.validation.binding; + +import android.databinding.BindingAdapter; +import android.widget.TextView; + +import br.com.ilhasoft.support.validation.R; + +/** + * Bindings for properties which configure validation behaviour + * + * + * Created by molexx on 12/11/2017. + */ +public class ConfigBindings { + + @BindingAdapter(value = {"showErrorMessage"}, requireAll = false) + public static void bindingShowErrorMessage(TextView view, boolean showErrorMessage) { + if (showErrorMessage == false) { + view.setTag(R.id.show_error_message, Boolean.FALSE); + } else { + view.setTag(R.id.show_error_message, null); + } + } + +} diff --git a/library/src/main/java/br/com/ilhasoft/support/validation/util/EditTextHandler.java b/library/src/main/java/br/com/ilhasoft/support/validation/util/EditTextHandler.java index 3200b4d..4222d07 100644 --- a/library/src/main/java/br/com/ilhasoft/support/validation/util/EditTextHandler.java +++ b/library/src/main/java/br/com/ilhasoft/support/validation/util/EditTextHandler.java @@ -38,12 +38,15 @@ public static void removeError(TextView textView) { } public static void setError(TextView textView, String errorMessage) { - TextInputLayout textInputLayout = getTextInputLayout(textView); - if (textInputLayout != null) { - textInputLayout.setErrorEnabled(!TextUtils.isEmpty(errorMessage)); - textInputLayout.setError(errorMessage); - } else { - textView.setError(errorMessage); + Boolean b = (Boolean)textView.getTag(R.id.show_error_message); + if (!Boolean.FALSE.equals(b)) { + TextInputLayout textInputLayout = getTextInputLayout(textView); + if (textInputLayout != null) { + textInputLayout.setErrorEnabled(!TextUtils.isEmpty(errorMessage)); + textInputLayout.setError(errorMessage); + } else { + textView.setError(errorMessage); + } } } diff --git a/library/src/main/res/values/ids.xml b/library/src/main/res/values/ids.xml index fde3aa6..dc3e8bb 100644 --- a/library/src/main/res/values/ids.xml +++ b/library/src/main/res/values/ids.xml @@ -2,4 +2,5 @@ + \ No newline at end of file From c70f3a490be389ed7601b1d67fb54a3443f1df0e Mon Sep 17 00:00:00 2001 From: molexx Date: Mon, 13 Nov 2017 00:32:11 +0000 Subject: [PATCH 2/2] check if the error message is different before calling setError() to avoid onscreen flickering no need to call textInputLayout.setErrorEnabled() as the SDK handles it --- .../support/validation/util/EditTextHandler.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/library/src/main/java/br/com/ilhasoft/support/validation/util/EditTextHandler.java b/library/src/main/java/br/com/ilhasoft/support/validation/util/EditTextHandler.java index 4222d07..12a0b05 100644 --- a/library/src/main/java/br/com/ilhasoft/support/validation/util/EditTextHandler.java +++ b/library/src/main/java/br/com/ilhasoft/support/validation/util/EditTextHandler.java @@ -42,10 +42,13 @@ public static void setError(TextView textView, String errorMessage) { if (!Boolean.FALSE.equals(b)) { TextInputLayout textInputLayout = getTextInputLayout(textView); if (textInputLayout != null) { - textInputLayout.setErrorEnabled(!TextUtils.isEmpty(errorMessage)); - textInputLayout.setError(errorMessage); + if ((TextUtils.isEmpty(errorMessage) && !TextUtils.isEmpty(textInputLayout.getError())) || (errorMessage != null && !errorMessage.equals(textInputLayout.getError()))) { + textInputLayout.setError(errorMessage); + } } else { - textView.setError(errorMessage); + if ((TextUtils.isEmpty(errorMessage) && !TextUtils.isEmpty(textView.getError())) || (errorMessage != null && !errorMessage.equals(textView.getError()))) { + textView.setError(errorMessage); + } } } }