diff --git a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/customviews/NativeEditText.java b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/customviews/NativeEditText.java index c1eab63c9..9f7186109 100644 --- a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/customviews/NativeEditText.java +++ b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/customviews/NativeEditText.java @@ -12,6 +12,8 @@ import com.rengwuxian.materialedittext.validation.METLengthChecker; import com.rengwuxian.materialedittext.validation.METValidator; +import org.apache.commons.lang3.StringUtils; + import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; @@ -156,7 +158,7 @@ public boolean validate() { } CharSequence text = getText(); - boolean isEmpty = text.length() == 0; + boolean isEmpty = StringUtils.isBlank(text); boolean isValid = true; for (final METValidator validator : validators) { diff --git a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/interactors/JsonFormInteractor.java b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/interactors/JsonFormInteractor.java index c6b30c4b6..6280427f5 100644 --- a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/interactors/JsonFormInteractor.java +++ b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/interactors/JsonFormInteractor.java @@ -221,30 +221,30 @@ public void fetchFields(List viewsFromJson, String stepName, JsonFormFragm private void fetchViews(final List viewsFromJson, final String stepName, final JsonFormFragment formFragment, final String type, final JSONObject jsonObject, final CommonListener listener, final Boolean popup) { - try { - formFragment.getJsonApi().getAppExecutors().mainThread().execute(new Runnable() { - @Override - public void run() { + formFragment.getJsonApi().getAppExecutors().mainThread().execute(new Runnable() { + @Override + public void run() { + try { FormWidgetFactory formWidgetFactory = map.get(type); if (formWidgetFactory != null) { List views = null; try { views = formWidgetFactory.getViewsFromJson(stepName, formFragment.getActivity(), formFragment, jsonObject, listener, popup); } catch (Exception e) { - e.printStackTrace(); + Timber.e(e, "Exception encountered in getViewsFromJsoo"); } - if (views.size() > 0) { + if (views != null && views.size() > 0) { viewsFromJson.addAll(views); } } + } catch (RuntimeException e) { + closeActivityAfterRuntimeException(formFragment, e); + } catch (Exception e) { + Timber.e(e, "Exception encountered while creating form widget!"); } - }); - - } catch (RuntimeException e) { - closeActivityAfterRuntimeException(formFragment, e); - } catch (Exception e) { - Timber.e(e, "Exception encountered while creating form widget!"); - } + } + }); +// } private void closeActivityAfterRuntimeException(JsonFormFragment jsonFormFragment, final RuntimeException e) { diff --git a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/widgets/EditTextFactory.java b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/widgets/EditTextFactory.java index d1dba9de1..b923fdfbb 100644 --- a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/widgets/EditTextFactory.java +++ b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/widgets/EditTextFactory.java @@ -13,6 +13,7 @@ import android.widget.RelativeLayout; import com.rengwuxian.materialedittext.MaterialEditText; +import com.rengwuxian.materialedittext.validation.METValidator; import com.rengwuxian.materialedittext.validation.RegexpValidator; import com.rey.material.util.ViewUtil; import com.vijay.jsonwizard.R; @@ -34,6 +35,7 @@ import com.vijay.jsonwizard.validators.edittext.RequiredValidator; import com.vijay.jsonwizard.views.JsonFormFragmentView; +import org.apache.commons.lang3.StringUtils; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @@ -67,10 +69,16 @@ public static ValidationStatus validate(JsonFormFragmentView formFragmentView, MaterialEditText editText) { if (editText.isEnabled()) { boolean validate = editText.validate(); - if (!validate) { + if (!validate || (StringUtils.isBlank(editText.getText()) && getRequiredValidator(editText) != null)) { String errorString = null; if (editText != null && editText.getError() != null) { errorString = editText.getError().toString(); + } else if (StringUtils.isBlank(editText.getText())) { + // Patching MaterialEditText to handle case + // when empty spaces are entered + METValidator validator = getRequiredValidator(editText); + errorString = validator.getErrorMessage(); + editText.setError(errorString); } return new ValidationStatus(false, errorString, formFragmentView, editText); } @@ -78,6 +86,18 @@ public static ValidationStatus validate(JsonFormFragmentView formFragmentView, return new ValidationStatus(true, null, formFragmentView, editText); } + private static METValidator getRequiredValidator(MaterialEditText editText) { + if (editText.getValidators() != null) { + for (METValidator validator : editText.getValidators()) { + if (validator instanceof RequiredValidator) { + return validator; + } + } + } + return null; + } + + @Override public List getViewsFromJson(String stepName, Context context, JsonFormFragment formFragment, JSONObject jsonObject, CommonListener diff --git a/gradle.properties b/gradle.properties index f212e50fa..18681682c 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,4 +1,4 @@ -VERSION_NAME=2.1.9-SNAPSHOT +VERSION_NAME=2.1.10-SNAPSHOT VERSION_CODE=1 GROUP=org.smartregister POM_SETTING_DESCRIPTION=OpenSRP Client Native Form Json Wizard