Skip to content

Commit c8b78eb

Browse files
authored
Merge pull request #471 from OpenSRP/add-relative-min-validator
Implement relative min validator
2 parents cb660d7 + 5d53066 commit c8b78eb

File tree

6 files changed

+95
-73
lines changed

6 files changed

+95
-73
lines changed

android-json-form-wizard/src/main/java/com/vijay/jsonwizard/constants/JsonFormConstants.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,10 @@ public class JsonFormConstants {
7474
public static final String V_MIN = "v_min";
7575
public static final String V_MAX = "v_max";
7676
public static final String V_RELATIVE_MAX = "v_relative_max";
77-
public static final String RELATIVE_MAX_VALIDATION_EXCEPTION = "exception";
77+
public static final String V_RELATIVE_MIN = "v_relative_min";
78+
public static final String RELATIVE_VALIDATION_EXCEPTION = "exception";
7879
public static final String DEFAULT_RELATIVE_MAX_VALIDATION_ERR = "Value cannot be higher than %s";
80+
public static final String DEFAULT_RELATIVE_MIN_VALIDATION_ERR = "Value cannot be less than %s";
7981
public static final String V_CUMULATIVE_TOTAL = "v_cumulative_total";
8082
public static final String DEFAULT_CUMULATIVE_VALIDATION_ERR = "Sum of %s and %s should be equal to %s";
8183
public static final String RELATED_FIELDS = "related_fields";

android-json-form-wizard/src/main/java/com/vijay/jsonwizard/validators/edittext/RelativeMaxNumericValidator.java

Lines changed: 0 additions & 55 deletions
This file was deleted.
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.vijay.jsonwizard.validators.edittext;
2+
3+
import android.support.annotation.NonNull;
4+
5+
import com.rengwuxian.materialedittext.validation.METValidator;
6+
import com.vijay.jsonwizard.constants.JsonFormConstants;
7+
import com.vijay.jsonwizard.fragments.JsonFormFragment;
8+
9+
import org.json.JSONArray;
10+
import org.json.JSONObject;
11+
12+
import timber.log.Timber;
13+
14+
import static com.vijay.jsonwizard.constants.JsonFormConstants.STEP1;
15+
import static com.vijay.jsonwizard.utils.FormUtils.fields;
16+
import static com.vijay.jsonwizard.utils.FormUtils.getFieldJSONObject;
17+
18+
/**
19+
* @author Vincent Karuri
20+
*/
21+
public class RelativeNumericValidator extends METValidator {
22+
23+
private JsonFormFragment formFragment;
24+
private String bindMaxValTo;
25+
private String step;
26+
private int exception;
27+
protected boolean isMaxValidator;
28+
29+
public RelativeNumericValidator(@NonNull String errorMessage, @NonNull JsonFormFragment formFragment, @NonNull String bindMaxValTo, int exception, String step, boolean isMaxValidator) {
30+
super(errorMessage);
31+
this.formFragment = formFragment;
32+
this.bindMaxValTo = bindMaxValTo;
33+
this.step = step == null ? STEP1 : step;
34+
this.exception = exception;
35+
this.isMaxValidator = isMaxValidator;
36+
}
37+
38+
public boolean isValid(@NonNull CharSequence text, boolean isEmpty) {
39+
return isValid(text, isEmpty, isMaxValidator);
40+
}
41+
42+
public boolean isValid(@NonNull CharSequence text, boolean isEmpty, boolean isMaxValidator) {
43+
if (!isEmpty) {
44+
try {
45+
JSONObject formJSONObject = new JSONObject(formFragment.getCurrentJsonState());
46+
JSONArray formFields = fields(formJSONObject, step);
47+
int relativeMaxFieldValue = getFieldJSONObject(formFields, bindMaxValTo).optInt(JsonFormConstants.VALUE);
48+
int currentTextValue = Integer.parseInt(text.toString());
49+
boolean isInvalid = isMaxValidator ? currentTextValue > relativeMaxFieldValue : currentTextValue < relativeMaxFieldValue;
50+
if (isInvalid && (currentTextValue != exception || exception == Integer.MIN_VALUE)) {
51+
return false;
52+
}
53+
} catch (Exception e) {
54+
Timber.e(e);
55+
return false;
56+
}
57+
}
58+
return true;
59+
}
60+
61+
62+
}
63+

android-json-form-wizard/src/main/java/com/vijay/jsonwizard/widgets/EditTextFactory.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
import com.vijay.jsonwizard.validators.edittext.MinLengthValidator;
3131
import com.vijay.jsonwizard.validators.edittext.MinNumericValidator;
3232
import com.vijay.jsonwizard.validators.edittext.ReferenceValidator;
33-
import com.vijay.jsonwizard.validators.edittext.RelativeMaxNumericValidator;
33+
import com.vijay.jsonwizard.validators.edittext.RelativeNumericValidator;
3434
import com.vijay.jsonwizard.validators.edittext.RequiredValidator;
3535
import com.vijay.jsonwizard.views.JsonFormFragmentView;
3636

@@ -45,12 +45,14 @@
4545

4646
import static com.vijay.jsonwizard.constants.JsonFormConstants.DEFAULT_CUMULATIVE_VALIDATION_ERR;
4747
import static com.vijay.jsonwizard.constants.JsonFormConstants.DEFAULT_RELATIVE_MAX_VALIDATION_ERR;
48+
import static com.vijay.jsonwizard.constants.JsonFormConstants.DEFAULT_RELATIVE_MIN_VALIDATION_ERR;
4849
import static com.vijay.jsonwizard.constants.JsonFormConstants.KEY;
4950
import static com.vijay.jsonwizard.constants.JsonFormConstants.RELATED_FIELDS;
50-
import static com.vijay.jsonwizard.constants.JsonFormConstants.RELATIVE_MAX_VALIDATION_EXCEPTION;
51+
import static com.vijay.jsonwizard.constants.JsonFormConstants.RELATIVE_VALIDATION_EXCEPTION;
5152
import static com.vijay.jsonwizard.constants.JsonFormConstants.STEP1;
5253
import static com.vijay.jsonwizard.constants.JsonFormConstants.V_CUMULATIVE_TOTAL;
5354
import static com.vijay.jsonwizard.constants.JsonFormConstants.V_RELATIVE_MAX;
55+
import static com.vijay.jsonwizard.constants.JsonFormConstants.V_RELATIVE_MIN;
5456
import static com.vijay.jsonwizard.utils.FormUtils.fields;
5557
import static com.vijay.jsonwizard.utils.FormUtils.getFieldJSONObject;
5658

@@ -150,7 +152,8 @@ protected void attachLayout(String stepName, Context context, JsonFormFragment f
150152
addUrlValidator(jsonObject, editText);
151153
addNumericValidator(jsonObject, editText);
152154
addNumericIntegerValidator(jsonObject, editText);
153-
addRelativeNumericIntegerValidator(jsonObject, formFragment, editText);
155+
addRelativeNumericIntegerValidator(jsonObject, formFragment, editText, true);
156+
addRelativeNumericIntegerValidator(jsonObject, formFragment, editText, false);
154157
addCumulativeTotalValidator(jsonObject, formFragment, editText, stepName, (JsonApi) context);
155158
// edit type check
156159
String editType = jsonObject.optString(JsonFormConstants.EDIT_TYPE);
@@ -304,8 +307,8 @@ private void addNumericIntegerValidator(JSONObject jsonObject, MaterialEditText
304307
}
305308

306309
private void addRelativeNumericIntegerValidator(JSONObject editTextJSONObject, JsonFormFragment formFragment,
307-
MaterialEditText editText) throws JSONException {
308-
JSONObject relativeMaxValidationJSONObject = editTextJSONObject.optJSONObject(V_RELATIVE_MAX);
310+
MaterialEditText editText, boolean isMaxValidator) throws JSONException {
311+
JSONObject relativeMaxValidationJSONObject = editTextJSONObject.optJSONObject(isMaxValidator ? V_RELATIVE_MAX : V_RELATIVE_MIN);
309312
if (relativeMaxValidationJSONObject != null) {
310313
// validate that the relative max field exists
311314
String relativeMaxValidationKey = relativeMaxValidationJSONObject.optString(JsonFormConstants.VALUE, null);
@@ -315,20 +318,21 @@ private void addRelativeNumericIntegerValidator(JSONObject editTextJSONObject, J
315318
if (relativeMaxFieldJSONObject != null) {
316319
// RELATIVE_MAX_VALIDATION_EXCEPTION, should never be set to Integer.MIN_VALUE in the native form json
317320
int relativeMaxValidationException = relativeMaxValidationJSONObject
318-
.optInt(RELATIVE_MAX_VALIDATION_EXCEPTION, 0);
321+
.optInt(RELATIVE_VALIDATION_EXCEPTION, 0);
319322
if (relativeMaxValidationException != Integer.MIN_VALUE) {
320323
// add validator
321324
String relativeMaxValidationErrorMsg = relativeMaxValidationJSONObject
322325
.optString(JsonFormConstants.ERR, null);
323-
String defaultErrMsg = String.format(DEFAULT_RELATIVE_MAX_VALIDATION_ERR, relativeMaxValidationKey);
326+
String defaultErrMsg = String.format(isMaxValidator ? DEFAULT_RELATIVE_MAX_VALIDATION_ERR : DEFAULT_RELATIVE_MIN_VALIDATION_ERR, relativeMaxValidationKey);
324327
relativeMaxValidationException = relativeMaxValidationJSONObject
325-
.optInt(RELATIVE_MAX_VALIDATION_EXCEPTION, Integer.MIN_VALUE);
326-
editText.addValidator(new RelativeMaxNumericValidator(
328+
.optInt(RELATIVE_VALIDATION_EXCEPTION, Integer.MIN_VALUE);
329+
editText.addValidator(new RelativeNumericValidator(
327330
relativeMaxValidationErrorMsg == null ? defaultErrMsg : relativeMaxValidationErrorMsg,
328331
formFragment,
329332
relativeMaxValidationKey,
330333
relativeMaxValidationException,
331-
STEP1));
334+
STEP1,
335+
isMaxValidator));
332336
}
333337
}
334338
}
Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
/**
1717
* Created by Vincent Karuri on 16/06/2020
1818
*/
19-
public class RelativeMaxNumericValidatorTest {
19+
public class RelativeNumericValidatorTest {
2020

2121
@Mock
2222
private JsonFormFragment formFragment;
@@ -95,10 +95,18 @@ public void setUp() throws JSONException {
9595
}
9696

9797
@Test
98-
public void testIsValidReturnsCorrectStatus() {
99-
RelativeMaxNumericValidator relativeMaxNumericValidator
100-
= new RelativeMaxNumericValidator("", formFragment, "key", 0, "step1");
101-
assertTrue(relativeMaxNumericValidator.isValid("1", false));
102-
assertFalse(relativeMaxNumericValidator.isValid("3", false));
98+
public void testIsMaxValidReturnsCorrectStatus() {
99+
RelativeNumericValidator relativeNumericValidator
100+
= new RelativeNumericValidator("", formFragment, "key", 0, "step1", true);
101+
assertTrue(relativeNumericValidator.isValid("1", false));
102+
assertFalse(relativeNumericValidator.isValid("3", false));
103+
}
104+
105+
@Test
106+
public void testIsMinValidReturnsCorrectStatus() {
107+
RelativeNumericValidator relativeNumericValidator
108+
= new RelativeNumericValidator("", formFragment, "key", 0, "step1", false);
109+
assertFalse(relativeNumericValidator.isValid("1", false));
110+
assertTrue(relativeNumericValidator.isValid("3", false));
103111
}
104112
}

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
VERSION_NAME=1.14.1-SNAPSHOT
1+
VERSION_NAME=1.14.2.0-SNAPSHOT
22
VERSION_CODE=1
33
GROUP=org.smartregister
44
POM_SETTING_DESCRIPTION=OpenSRP Client Native Form Json Wizard

0 commit comments

Comments
 (0)