Skip to content

Commit

Permalink
Merge pull request #603 from opensrp/fix-edittext-validation
Browse files Browse the repository at this point in the history
Fix EditText Validation
  • Loading branch information
hamza-vd authored Sep 24, 2021
2 parents 64081d2 + 2786af4 commit c69b63c
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,30 +221,30 @@ public void fetchFields(List<View> viewsFromJson, String stepName, JsonFormFragm

private void fetchViews(final List<View> 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<View> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -67,17 +69,35 @@ 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);
}
}
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<View> getViewsFromJson(String stepName, Context context, JsonFormFragment formFragment,
JSONObject jsonObject, CommonListener
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.1.9-SNAPSHOT
VERSION_NAME=2.1.10-SNAPSHOT
VERSION_CODE=1
GROUP=org.smartregister
POM_SETTING_DESCRIPTION=OpenSRP Client Native Form Json Wizard
Expand Down

0 comments on commit c69b63c

Please sign in to comment.