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

Min/Max value check #221

Open
thawhtetkaung opened this issue Aug 14, 2018 · 2 comments
Open

Min/Max value check #221

thawhtetkaung opened this issue Aug 14, 2018 · 2 comments

Comments

@thawhtetkaung
Copy link

How to check min fields is not greater than max fields?

@blu94
Copy link

blu94 commented Feb 10, 2019

i'm curios on this too

@thawhtetkaung
Copy link
Author

Since there is no answer about rule. I comment that how I bypass that situation. I create a quick rule to check and add that to validator.
Here is sample codes.
MinMaxQuickRule

import android.content.Context;
import android.view.View;
import android.widget.EditText;
import com.mobsandgeeks.saripaar.QuickRule;
import static java.lang.Double.parseDouble;
import static java.lang.Integer.parseInt;


public class MinMaxQuickRule extends QuickRule<EditText> {

    private EditText min;
    private EditText max;
    private String message;

    public MinMaxQuickRule(int sequence, String message, EditText min, EditText max) {
        super(sequence);
        this.min = min;
        this.max = max;
        this.message = message;
    }

    @Override
    public boolean isValid(EditText editText) {

        String minString = this.min.getText().toString();
        String maxString = this.min.getText().toString();

        // Check min and max is not insert
        if (minString.equals("") && maxString.equals("")) {
            return true;
        }

        try {
            int min = parseInt(this.min.getText().toString());
            int max = parseInt(this.max.getText().toString());
            if (min < max) return true;
        } catch (NumberFormatException notInt) {
            try {
                double min = parseDouble(this.min.getText().toString());
                double max = parseDouble(this.max.getText().toString());
                if (min < max) return true;
            } catch (NumberFormatException notDouble) {
                return false;
            }
        }
        return false;
    }

    @Override
    public String getMessage(Context context) {
        return message;
    }
}

Custom Validator Class

import android.widget.EditText;

import com.mobsandgeeks.saripaar.Validator;
import com.mobsandgeeks.saripaar.annotation.DecimalMax;
import com.mobsandgeeks.saripaar.annotation.DecimalMin;
import com.mobsandgeeks.saripaar.annotation.Max;
import com.mobsandgeeks.saripaar.annotation.Min;
import com.mobsandgeeks.saripaar.annotation.NotEmpty;
import com.mobsandgeeks.saripaar.annotation.Order;
import com.mobsandgeeks.saripaar.annotation.Pattern;



    public class HelloWroldValidator {
        @Order(1)
        @NotEmpty(sequence = 1, message = "NotEmpty")
        @DecimalMin(value = 0, sequence = 2, message = "DecimalMin")
        @DecimalMax(value = 999, sequence = 3, message = "DecimalMax")
        EditText minExitText;

        @Order(2)
        @NotEmpty(sequence = 1, message = "NotEmpty")
        @DecimalMin(value = 0, sequence = 2, message = "DecimalMin")
        @DecimalMax(value = 999, sequence = 3, message = "DecimalMax")
        EditText maxExitText;

        private Validator validator;
        private String message = "Min Should be smaller than max";


        public POAlertValidator(EditText minExitText, EditText maxExitText, Validator.ValidationListener listener) {
            this.minExitText = minExitText;
            this.maxExitText = maxExitText;
            validator = new Validator(this);
            validator.put(maxExitText, new MinMaxQuickRule(3, message, minExitText, maxExitText));
            validator.setValidationListener(listener);
        }

        public void validate() {
            validator.validate();
        }
    }

Use validator in Activity

.
.
.
// Init Validator 
helloWorldValidator = new HelloWroldValidator(min, max, new Validator.ValidationListener() {
            @SuppressLint("SetTextI18n")
            @Override
            public void onValidationSucceeded() {
                //Do Success Thing
            }

            @Override
            public void onValidationFailed(List<ValidationError> errors) {
               // Do fail Thing.
            }
        });
.
.
.
// Make Validation 
helloWorldValidator .validate();

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

2 participants