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..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 @@ -38,12 +38,18 @@ 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) { + if ((TextUtils.isEmpty(errorMessage) && !TextUtils.isEmpty(textInputLayout.getError())) || (errorMessage != null && !errorMessage.equals(textInputLayout.getError()))) { + textInputLayout.setError(errorMessage); + } + } else { + if ((TextUtils.isEmpty(errorMessage) && !TextUtils.isEmpty(textView.getError())) || (errorMessage != null && !errorMessage.equals(textView.getError()))) { + 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