Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to recheck field in android-saripaar?

I use the android-saripaar library.

I can check all the EditText fields on the correctness of the entered data using validate(). If I fill the EditText on with a mistake in correct data, the error doesn't disappear as long as I again call a validate().

How can I check the validity after entering text only in a certain EditText and to remove a mistake from it?

Thanks.

like image 434
Mark Korzhov Avatar asked Jun 02 '15 05:06

Mark Korzhov


1 Answers

You can use Saripaar's Mode.IMMEDIATE in combination with TextWatcher instances to accomplish what you are looking for.

  1. Use the @Order annotation one every view fields that have Saripaar annotations. This is required to use immediate validation mode. If you want to validate rules in a specific order, each saripaar annotation has a sequence attribute. The sequence attribute is optional, but very helpful if you want the rules to be validated in a specific sequence.
    @Order(1)
    @NotEmpty(sequence = 1)
    @Email(seqence = 2)
    private EditText mEmailEditText;

    @Order(2)
    @Password
    private EditText mPasswordEditText;
  1. Set the mode to immediate mValidator.setMode(Mode.IMMEDIATE)

  2. Add TextWatchers / appropriate listeners to your widgets.

    TextWatcher validationTextWatcher = new TextWatcher() {

        @Override
        void afterTextChanged(Editable s) {
            mValidator.validate();
        }

        // Other methods to override ...
    }

    mEmailEditText.addTextChangedListener(validationTextWatcher);
    mPasswordEditText.addTextChangedListener(validationTextWatcher);
  1. (Optional) There's a default ViewValidatedAction implementation that will automatically clear errors on EditText fields when it is valid. If your form uses alternate error display mechanism / widgets, you can clear them by specifying your own ViewValidatedAction instance.
    mValidator.setViewValidatedAction(new ViewValidatedAction() {

        @Override
        void onAllRulesPassed(View view) {
            // ... clear errors based on the view type
        }
    });

Disclosure: I'm the author of Saripaar

like image 82
Ragunath Jawahar Avatar answered Sep 28 '22 05:09

Ragunath Jawahar