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

Added array validator to check if already registered birth certificate is entered #564

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import com.shashank.sony.fancydialoglib.FancyAlertDialog;
import com.shashank.sony.fancydialoglib.Icon;
import com.vijay.jsonwizard.R;
import com.vijay.jsonwizard.comparisons.ArrayComparison;
import com.vijay.jsonwizard.comparisons.Comparison;
import com.vijay.jsonwizard.comparisons.EqualToComparison;
import com.vijay.jsonwizard.comparisons.GreaterThanComparison;
Expand Down Expand Up @@ -1452,6 +1453,10 @@ private void initComparisons() {
RegexComparison regexComparison = new RegexComparison();
functionRegex += "|" + regexComparison.getFunctionName();
comparisons.put(regexComparison.getFunctionName(), regexComparison);

ArrayComparison arrayComparison = new ArrayComparison();
functionRegex += "|" + arrayComparison.getFunctionName();
comparisons.put(arrayComparison.getFunctionName(), arrayComparison);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.vijay.jsonwizard.comparisons;

import android.util.Log;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ArrayComparison extends Comparison {
private final String TAG = this.getClass().getSimpleName();

@Override
public boolean compare(String a, String type, String b) {
if (a != null && b != null) {
try {
Pattern pattern = Pattern.compile(b);
Matcher matcher = pattern.matcher(a);
return matcher.matches();
} catch (Exception e) {
Log.e(TAG, Log.getStackTraceString(e));
}
}
return false;
}

@Override
public String getFunctionName() {
return "array";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public class JsonFormConstants {
public static final String V_MAX_LENGTH = "v_max_length";
public static final String IS_FIXED_SIZE = "is_fixed_size";
public static final String V_REGEX = "v_regex";
public static final String V_ARRAY = "v_array";
public static final String V_EMAIL = "v_email";
public static final String V_URL = "v_url";
public static final String V_NUMERIC_INTEGER = "v_numeric_integer";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ private void registerDefaultTranslatableFields() {
defaultTranslatableWidgetFields.add(JsonFormConstants.HINT);
defaultTranslatableWidgetFields.add(JsonFormConstants.V_REQUIRED + "." + JsonFormConstants.ERR);
defaultTranslatableWidgetFields.add(JsonFormConstants.V_REGEX + "." + JsonFormConstants.ERR);
defaultTranslatableWidgetFields.add(JsonFormConstants.V_ARRAY + "." + JsonFormConstants.ERR);
defaultTranslatableWidgetFields.add(JsonFormConstants.V_NUMERIC + "." + JsonFormConstants.ERR);
defaultTranslatableWidgetFields.add(JsonFormConstants.V_NUMERIC_INTEGER + "." + JsonFormConstants.ERR);
defaultTranslatableWidgetFields.add(JsonFormConstants.V_MIN + "." + JsonFormConstants.ERR);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ public void run() {
addRequiredValidator(jsonObject, editText);
addLengthValidator(jsonObject, editText);
addRegexValidator(jsonObject, editText);
addArrayValidator(jsonObject, editText);
addEmailValidator(jsonObject, editText);
addUrlValidator(jsonObject, editText);
addNumericValidator(jsonObject, editText);
Expand Down Expand Up @@ -243,6 +244,16 @@ private void addRegexValidator(JSONObject jsonObject, MaterialEditText editText)
}
}

private void addArrayValidator(JSONObject jsonObject, MaterialEditText editText) throws JSONException {
JSONObject arrayObject = jsonObject.optJSONObject(JsonFormConstants.V_ARRAY);
if (arrayObject != null) {
String regexValue = arrayObject.optString(JsonFormConstants.VALUE);
if (!TextUtils.isEmpty(regexValue)) {
editText.addValidator(new RegexpValidator(arrayObject.getString(JsonFormConstants.ERR), regexValue));
}
}
}

private void addEmailValidator(JSONObject jsonObject, MaterialEditText editText) throws JSONException {
JSONObject emailObject = jsonObject.optJSONObject(JsonFormConstants.V_EMAIL);
if (emailObject != null) {
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION_NAME=2.0.7-SNAPSHOT
VERSION_NAME=2.0.8-SNAPSHOT
VERSION_CODE=1
GROUP=org.smartregister
POM_SETTING_DESCRIPTION=OpenSRP Client Native Form Json Wizard
Expand Down