Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only show one message error with each validation process #214

Open
srinnix1395 opened this issue Mar 15, 2018 · 2 comments
Open

Only show one message error with each validation process #214

srinnix1395 opened this issue Mar 15, 2018 · 2 comments

Comments

@srinnix1395
Copy link

srinnix1395 commented Mar 15, 2018

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
@TikTak123
Copy link

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

@myroniak
Copy link

myroniak commented Jul 14, 2020

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();
               }
           }
       }
   }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants