Open thawhtetkaung opened 6 years ago
i'm curios on this too
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();
How to check min fields is not greater than max fields?