Skip to content
This repository was archived by the owner on Nov 5, 2025. It is now read-only.

Commit 882755b

Browse files
author
John Cordeiro
committed
Add custom error message capability
1 parent f8cda41 commit 882755b

22 files changed

Lines changed: 163 additions & 71 deletions

library/src/main/java/br/com/ilhasoft/support/validation/binding/DateBindings.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,21 @@
66
import br.com.ilhasoft.support.validation.R;
77
import br.com.ilhasoft.support.validation.rule.DateRule;
88
import br.com.ilhasoft.support.validation.util.EditTextHandler;
9+
import br.com.ilhasoft.support.validation.util.ErrorMessageHelper;
910
import br.com.ilhasoft.support.validation.util.ViewTagHelper;
1011

1112
/**
1213
* Created by john-mac on 6/16/16.
1314
*/
1415
public class DateBindings {
1516

16-
@BindingAdapter({"validateDate"})
17-
public static void bindingDate(TextView view, String pattern) {
17+
@BindingAdapter(value = {"validateDate", "validateDateMessage"}, requireAll = false)
18+
public static void bindingDate(TextView view, String pattern, String errorMessage) {
1819
EditTextHandler.disableErrorOnChanged(view);
19-
ViewTagHelper.appendValue(R.id.validator_rule, view, new DateRule(view, pattern));
20+
21+
String handledErrorMessage = ErrorMessageHelper.getStringOrDefault(view,
22+
errorMessage, R.string.error_message_date_validation);
23+
ViewTagHelper.appendValue(R.id.validator_rule, view, new DateRule(view, pattern, handledErrorMessage));
2024
}
2125

2226
}

library/src/main/java/br/com/ilhasoft/support/validation/binding/LengthBindings.java

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,39 @@
99
import br.com.ilhasoft.support.validation.rule.MaxLengthRule;
1010
import br.com.ilhasoft.support.validation.rule.MinLengthRule;
1111
import br.com.ilhasoft.support.validation.util.EditTextHandler;
12+
import br.com.ilhasoft.support.validation.util.ErrorMessageHelper;
1213
import br.com.ilhasoft.support.validation.util.ViewTagHelper;
1314

1415
/**
1516
* Created by john-mac on 5/14/16.
1617
*/
1718
public class LengthBindings {
1819

19-
@BindingAdapter({"validateMinLength"})
20-
public static void bindingMinLength(TextView view, int minLength) {
20+
@BindingAdapter(value = {"validateMinLength", "validateMinLengthMessage"}, requireAll = false)
21+
public static void bindingMinLength(TextView view, int minLength, CharSequence errorMessage) {
2122
EditTextHandler.disableErrorOnChanged(view);
22-
ViewTagHelper.appendValue(R.id.validator_rule, view, new MinLengthRule(view, minLength));
23+
24+
String handledErrorMessage = ErrorMessageHelper.getStringOrDefault(view,
25+
errorMessage, R.string.error_message_min_length, minLength);
26+
ViewTagHelper.appendValue(R.id.validator_rule, view, new MinLengthRule(view, minLength, handledErrorMessage));
2327
}
2428

25-
@BindingAdapter({"validateMaxLength"})
26-
public static void bindingMaxLength(TextView view, int maxLength) {
29+
@BindingAdapter(value = {"validateMaxLength", "validateMaxLengthMessage"}, requireAll = false)
30+
public static void bindingMaxLength(TextView view, int maxLength, CharSequence errorMessage) {
2731
EditTextHandler.disableErrorOnChanged(view);
28-
ViewTagHelper.appendValue(R.id.validator_rule, view, new MaxLengthRule(view, maxLength));
32+
33+
String handledErrorMessage = ErrorMessageHelper.getStringOrDefault(view,
34+
errorMessage, R.string.error_message_max_length, maxLength);
35+
ViewTagHelper.appendValue(R.id.validator_rule, view, new MaxLengthRule(view, maxLength, handledErrorMessage));
2936
}
3037

31-
@BindingAdapter({"validateEmpty"})
32-
public static void bindingEmpty(TextView view, boolean empty) {
38+
@BindingAdapter(value = {"validateEmpty", "validateEmptyMessage"}, requireAll = false)
39+
public static void bindingEmpty(TextView view, boolean empty, CharSequence errorMessage) {
3340
EditTextHandler.disableErrorOnChanged(view);
34-
ViewTagHelper.appendValue(R.id.validator_rule, view, new EmptyRule(view, empty));
41+
42+
String handledErrorMessage = ErrorMessageHelper.getStringOrDefault(view,
43+
errorMessage, R.string.error_message_empty_validation);
44+
ViewTagHelper.appendValue(R.id.validator_rule, view, new EmptyRule(view, empty, handledErrorMessage));
3545
}
3646

3747
}

library/src/main/java/br/com/ilhasoft/support/validation/binding/PasswordBindings.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,22 @@
66
import br.com.ilhasoft.support.validation.R;
77
import br.com.ilhasoft.support.validation.rule.ConfirmPasswordRule;
88
import br.com.ilhasoft.support.validation.util.EditTextHandler;
9+
import br.com.ilhasoft.support.validation.util.ErrorMessageHelper;
910
import br.com.ilhasoft.support.validation.util.ViewTagHelper;
1011

1112
/**
1213
* Created by felipe on 22/12/16.
1314
*/
1415
public class PasswordBindings {
1516

16-
@BindingAdapter("validatePassword")
17-
public static void bindingPassword(TextView view, TextView comparableView) {
17+
@BindingAdapter(value = {"validatePassword", "validatePasswordMessage"}, requireAll = false)
18+
public static void bindingPassword(TextView view, TextView comparableView, String errorMessage) {
1819
EditTextHandler.disableErrorOnChanged(view);
20+
21+
String handledErrorMessage = ErrorMessageHelper.getStringOrDefault(view,
22+
errorMessage, R.string.error_message_not_equal_password);
1923
ViewTagHelper.appendValue(R.id.validator_rule, view,
20-
new ConfirmPasswordRule(view, comparableView));
24+
new ConfirmPasswordRule(view, comparableView, handledErrorMessage));
2125
}
2226

2327
}

library/src/main/java/br/com/ilhasoft/support/validation/binding/TypeBindings.java

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,27 @@
22

33
import android.databinding.BindingAdapter;
44
import android.support.annotation.NonNull;
5-
import android.text.Editable;
6-
import android.text.TextWatcher;
7-
import android.util.Log;
8-
import android.view.View;
95
import android.widget.TextView;
106

11-
import java.lang.reflect.InvocationTargetException;
12-
137
import br.com.ilhasoft.support.validation.R;
14-
import br.com.ilhasoft.support.validation.rule.EmailTypeRule;
158
import br.com.ilhasoft.support.validation.rule.TypeRule;
16-
import br.com.ilhasoft.support.validation.rule.UrlTypeRule;
179
import br.com.ilhasoft.support.validation.util.EditTextHandler;
10+
import br.com.ilhasoft.support.validation.util.ErrorMessageHelper;
1811
import br.com.ilhasoft.support.validation.util.ViewTagHelper;
1912

2013
/**
2114
* Created by john-mac on 5/14/16.
2215
*/
2316
public class TypeBindings {
2417

25-
@BindingAdapter({"validateType"})
26-
public static void bindingTypeValidation(TextView view, String fieldTypeText) {
18+
@BindingAdapter(value = {"validateType", "validateTypeMessage"}, requireAll = false)
19+
public static void bindingTypeValidation(TextView view, String fieldTypeText, String errorMessage) {
2720
EditTextHandler.disableErrorOnChanged(view);
2821
TypeRule.FieldType fieldType = getFieldTypeByText(fieldTypeText);
2922
try {
30-
ViewTagHelper.appendValue(R.id.validator_rule, view, fieldType.instantiate(view));
23+
String handledErrorMessage = ErrorMessageHelper.getStringOrDefault(view,
24+
errorMessage, fieldType.errorMessageId);
25+
ViewTagHelper.appendValue(R.id.validator_rule, view, fieldType.instantiate(view, handledErrorMessage));
3126
} catch (Exception ignored) {}
3227
}
3328

library/src/main/java/br/com/ilhasoft/support/validation/rule/ConfirmPasswordRule.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
*/
1111
public class ConfirmPasswordRule extends Rule<TextView, TextView> {
1212

13-
public ConfirmPasswordRule(TextView view, TextView value) {
14-
super(view, value);
13+
public ConfirmPasswordRule(TextView view, TextView value, String errorMessage) {
14+
super(view, value, errorMessage);
1515
}
1616

1717
@Override
@@ -30,7 +30,6 @@ public void onValidationSucceeded(TextView view) {
3030

3131
@Override
3232
public void onValidationFailed(TextView view) {
33-
final String message = view.getContext().getString(R.string.error_message_not_equal_password);
34-
EditTextHandler.setError(view, message);
33+
EditTextHandler.setError(view, errorMessage);
3534
}
3635
}

library/src/main/java/br/com/ilhasoft/support/validation/rule/CpfTypeRule.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,15 @@
22

33
import android.widget.TextView;
44

5-
import br.com.ilhasoft.support.validation.R;
65
import br.com.ilhasoft.support.validation.util.EditTextHandler;
76

87
/**
98
* Created by john-mac on 5/14/16.
109
*/
1110
public class CpfTypeRule extends TypeRule {
1211

13-
public CpfTypeRule(TextView view) {
14-
super(view, FieldType.Cpf);
12+
public CpfTypeRule(TextView view, String errorMessage) {
13+
super(view, FieldType.Cpf, errorMessage);
1514
}
1615

1716
@Override
@@ -52,6 +51,6 @@ protected void onValidationSucceeded(TextView view) {
5251
@Override
5352
protected void onValidationFailed(TextView view) {
5453
super.onValidationFailed(view);
55-
EditTextHandler.setError(view, view.getContext().getString(R.string.error_message_cpf_validation));
54+
EditTextHandler.setError(view, errorMessage);
5655
}
5756
}

library/src/main/java/br/com/ilhasoft/support/validation/rule/CreditCardTypeRule.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import org.apache.commons.validator.routines.CreditCardValidator;
66

7-
import br.com.ilhasoft.support.validation.R;
87
import br.com.ilhasoft.support.validation.util.EditTextHandler;
98

109
/**
@@ -14,8 +13,8 @@ public class CreditCardTypeRule extends TypeRule {
1413

1514
private final CreditCardValidator creditCardValidator;
1615

17-
public CreditCardTypeRule(TextView view) {
18-
super(view, FieldType.CreditCard);
16+
public CreditCardTypeRule(TextView view, String errorMessage) {
17+
super(view, FieldType.CreditCard, errorMessage);
1918
creditCardValidator = new CreditCardValidator();
2019
}
2120

@@ -33,7 +32,7 @@ protected void onValidationSucceeded(TextView view) {
3332
@Override
3433
protected void onValidationFailed(TextView view) {
3534
super.onValidationFailed(view);
36-
EditTextHandler.setError(view, view.getContext().getString(R.string.error_message_credit_card_validation));
35+
EditTextHandler.setError(view, errorMessage);
3736
}
3837

3938
}

library/src/main/java/br/com/ilhasoft/support/validation/rule/DateRule.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import org.apache.commons.validator.routines.DateValidator;
66

7-
import br.com.ilhasoft.support.validation.R;
87
import br.com.ilhasoft.support.validation.util.EditTextHandler;
98

109
/**
@@ -14,8 +13,8 @@ public class DateRule extends Rule<TextView, String> {
1413

1514
private final DateValidator dateValidator;
1615

17-
public DateRule(TextView view, String value) {
18-
super(view, value);
16+
public DateRule(TextView view, String value, String errorMessage) {
17+
super(view, value, errorMessage);
1918
dateValidator = new DateValidator();
2019
}
2120

@@ -31,6 +30,6 @@ public void onValidationSucceeded(TextView view) {
3130

3231
@Override
3332
public void onValidationFailed(TextView view) {
34-
EditTextHandler.setError(view, view.getContext().getString(R.string.error_message_date_validation));
33+
EditTextHandler.setError(view, errorMessage);
3534
}
3635
}

library/src/main/java/br/com/ilhasoft/support/validation/rule/EmailTypeRule.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
*/
1414
public class EmailTypeRule extends TypeRule {
1515

16-
public EmailTypeRule(TextView view) {
17-
super(view, FieldType.Email);
16+
public EmailTypeRule(TextView view, String errorMessage) {
17+
super(view, FieldType.Email, errorMessage);
1818
}
1919

2020
@Override
@@ -32,6 +32,6 @@ protected void onValidationSucceeded(TextView view) {
3232
@Override
3333
protected void onValidationFailed(TextView view) {
3434
super.onValidationFailed(view);
35-
EditTextHandler.setError(view, view.getContext().getString(R.string.error_message_email_validation));
35+
EditTextHandler.setError(view, errorMessage);
3636
}
3737
}

library/src/main/java/br/com/ilhasoft/support/validation/rule/EmptyRule.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
*/
1212
public class EmptyRule extends Rule<TextView, Boolean> {
1313

14-
public EmptyRule(TextView view, Boolean value) {
15-
super(view, value);
14+
public EmptyRule(TextView view, Boolean value, String errorMessage) {
15+
super(view, value, errorMessage);
1616
}
1717

1818
@Override
@@ -27,6 +27,6 @@ public void onValidationSucceeded(TextView view) {
2727

2828
@Override
2929
public void onValidationFailed(TextView view) {
30-
EditTextHandler.setError(view, view.getContext().getString(R.string.error_message_empty_validation));
30+
EditTextHandler.setError(view, errorMessage);
3131
}
3232
}

0 commit comments

Comments
 (0)