android-saripaar icon indicating copy to clipboard operation
android-saripaar copied to clipboard

Only show one message error with each validation process

Open srinnix1395 opened this issue 6 years ago • 2 comments

I am using 3 validtion on one EditText. When the validation is completed, I use getCollatedErrorMessage function, but that function return 3 error (3 error appended). I want the function can get one error. And the order of validation will be handled in an order, eg: NotEmpty, Length, Email, if one condition failed, I want get the corresponding error. How can I do?

@NotEmpty(messageResId = R.string.AP5007_VALIDATION_1_1)
@Length(max = 64, messageResId = R.string.AP5007_VALIDATION_1_2)
@Email(messageResId = R.string.AP5007_VALIDATION_1_3)
@BindView(R.id.edt_email)
lateinit var edtEmail: EditText

srinnix1395 avatar Mar 15 '18 09:03 srinnix1395

Hi, @srinnix1395 I have the same problem. Have you solved this problem?

TikTak123 avatar Aug 03 '18 10:08 TikTak123

I have a solution and it works fine for me.

First of all, you need to set a sequence. In my case first message to show it's NotEmpty

@NotEmpty(sequence = 1, messageResId = R.string.error_blank_email)
@Email(sequence = 2, messageResId = R.string.error_invalid_email)
@BindView(R.id.et_email)
protected TextInputEditText emailInput;

After that, you need to overwrite the default method getCollatedErrorMessage(Context context) to:

 public static String getCollatedErrorMessage(final Context context, List<Rule> failedRules) {
        return failedRules.get(0).getMessage(context);
    }

The last step is to call a message in onValidationFailed() method :

@Override
   public void onValidationFailed(List<ValidationError> errors) {
       for (ValidationError error : errors) {
           View view = error.getView();
          // String message = error.getCollatedErrorMessage(this);
           **String message = getCollatedErrorMessage(this, error.getFailedRules());**

           if (view instanceof EditText) {
                      ...
               } else {
                   Toast.makeText(this, message, Toast.LENGTH_LONG).show();
               }
           }
       }
   }

myroniak avatar Jul 14 '20 11:07 myroniak