ragunathjawahar / android-saripaar

UI form validation library for Android
Apache License 2.0
3.22k stars 460 forks source link

Only show one message error with each validation process #214

Open srinnix1395 opened 6 years ago

srinnix1395 commented 6 years ago

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 commented 6 years ago

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

myroniak commented 4 years ago

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