Skip to content

Commit

Permalink
fix: Group, Center, Client Name Validation during Creation (openMF#372)
Browse files Browse the repository at this point in the history
fix: Method Names
  • Loading branch information
therajanmaurya authored and droidchef committed Oct 3, 2016
1 parent 690ce53 commit 7c8d991
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import com.mifos.services.data.CenterPayload;
import com.mifos.utils.DateHelper;
import com.mifos.utils.FragmentConstants;
import com.mifos.utils.ValidationUtil;

import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -148,7 +149,7 @@ private void inflateOfficeSpinner() {

private void initiateCenterCreation(CenterPayload centerPayload) {

if (isValidCenterName()) {
if (isCenterNameValid()) {
mCreateNewCenterPresenter.createCenter(centerPayload);
}
}
Expand Down Expand Up @@ -176,7 +177,8 @@ public void onDatePicked(String date) {

}

public boolean isValidCenterName() {
public boolean isCenterNameValid() {
result = true;
try {
if (TextUtils.isEmpty(et_centerName.getEditableText().toString())) {
throw new RequiredFieldException(getResources().getString(R.string.center_name),
Expand All @@ -187,10 +189,11 @@ public boolean isValidCenterName() {
.getEditableText().toString().trim().length() > 0) {
throw new ShortOfLengthException(getResources().getString(R.string.center_name), 4);
}
if (!et_centerName.getEditableText().toString().matches("[a-zA-Z]+")) {
throw new InvalidTextInputException(getResources().getString(R.string
.center_name), getResources().getString(R.string
.error_should_contain_only), InvalidTextInputException.TYPE_ALPHABETS);
if (!ValidationUtil.isNameValid(et_centerName.getEditableText().toString())) {
throw new InvalidTextInputException(
getResources().getString(R.string.center_name),
getResources().getString(R.string.error_should_contain_only),
InvalidTextInputException.TYPE_ALPHABETS);
}
} catch (InvalidTextInputException e) {
e.notifyUserWithToast(getActivity());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import android.widget.TextView;
import android.widget.Toast;

import com.mifos.objects.client.ClientPayload;
import com.mifos.exceptions.InvalidTextInputException;
import com.mifos.exceptions.RequiredFieldException;
import com.mifos.exceptions.ShortOfLengthException;
Expand All @@ -33,12 +32,14 @@
import com.mifos.mifosxdroid.core.util.Toaster;
import com.mifos.mifosxdroid.uihelpers.MFDatePicker;
import com.mifos.objects.client.Client;
import com.mifos.objects.client.ClientPayload;
import com.mifos.objects.organisation.Office;
import com.mifos.objects.organisation.Staff;
import com.mifos.objects.templates.clients.ClientsTemplate;
import com.mifos.objects.templates.clients.Options;
import com.mifos.utils.DateHelper;
import com.mifos.utils.FragmentConstants;
import com.mifos.utils.ValidationUtil;

import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -174,7 +175,9 @@ public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
public void onClick(View view) {
ClientPayload clientPayload = new ClientPayload();
clientPayload.setFirstname(et_clientFirstName.getEditableText().toString());
clientPayload.setMiddlename(et_clientMiddleName.getEditableText().toString());
if (!TextUtils.isEmpty(et_clientMiddleName.getEditableText().toString())) {
clientPayload.setMiddlename(et_clientMiddleName.getEditableText().toString());
}
clientPayload.setMobileNo(et_clientMobileNo.getEditableText().toString());
clientPayload.setExternalId(et_clientexternalId.getEditableText().toString());
clientPayload.setLastname(et_clientLastName.getEditableText().toString());
Expand Down Expand Up @@ -299,13 +302,13 @@ public void inflateStaffSpinner(final int officeId) {

private void initiateClientCreation(ClientPayload clientPayload) {

if (!isValidFirstName()) {
if (!isFirstNameValid()) {
return;
}
if (!isValidMiddleName()) {
if (!isMiddleNameValid()) {
return;
}
if (isValidLastName()) {
if (isLastNameValid()) {

mCreateNewClientPresenter.createClient(clientPayload);
}
Expand Down Expand Up @@ -353,7 +356,8 @@ public void onDatePicked(String date) {

}

public boolean isValidFirstName() {
public boolean isFirstNameValid() {
result = true;
try {
if (TextUtils.isEmpty(et_clientFirstName.getEditableText().toString())) {
throw new RequiredFieldException(getResources().getString(R.string.first_name),
Expand All @@ -363,9 +367,9 @@ public boolean isValidFirstName() {
et_clientFirstName.getEditableText().toString().trim().length() > 0) {
throw new ShortOfLengthException(getResources().getString(R.string.first_name), 4);
}
if (!et_clientFirstName.getEditableText().toString().matches("[a-zA-Z]+")) {
throw new InvalidTextInputException(getResources().getString(R.string.first_name)
, getResources().getString(R.string.error_should_contain_only),
if (!ValidationUtil.isNameValid(et_clientFirstName.getEditableText().toString())) {
throw new InvalidTextInputException(getResources().getString(R.string.first_name),
getResources().getString(R.string.error_should_contain_only),
InvalidTextInputException.TYPE_ALPHABETS);
}
} catch (InvalidTextInputException e) {
Expand All @@ -381,22 +385,25 @@ public boolean isValidFirstName() {
return result;
}

public boolean isValidMiddleName() {
public boolean isMiddleNameValid() {
result = true;
try {
if (!et_clientMiddleName.getEditableText().toString().matches("[a-zA-Z]+")) {
throw new InvalidTextInputException(getResources().getString(R.string
.middle_name), getResources().getString(R.string
.error_should_contain_only), InvalidTextInputException.TYPE_ALPHABETS);
if (!TextUtils.isEmpty(et_clientMiddleName.getEditableText().toString())
&& !ValidationUtil.isNameValid(et_clientMiddleName.getEditableText()
.toString())) {
throw new InvalidTextInputException(
getResources().getString(R.string.middle_name),
getResources().getString(R.string.error_should_contain_only),
InvalidTextInputException.TYPE_ALPHABETS);
}
} catch (InvalidTextInputException e) {
e.notifyUserWithToast(getActivity());
result = false;
}

return result;
}

public boolean isValidLastName() {
public boolean isLastNameValid() {
result = true;
try {
if (TextUtils.isEmpty(et_clientLastName.getEditableText().toString())) {
Expand All @@ -409,7 +416,7 @@ public boolean isValidLastName() {
throw new ShortOfLengthException(getResources().getString(R.string.last_name), 4);
}

if (!et_clientLastName.getEditableText().toString().matches("[a-zA-Z]+")) {
if (!ValidationUtil.isNameValid(et_clientLastName.getEditableText().toString())) {
throw new InvalidTextInputException(getResources().getString(R.string.last_name),
getResources().getString(R.string.error_should_contain_only),
InvalidTextInputException.TYPE_ALPHABETS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@
import com.mifos.mifosxdroid.core.ProgressableFragment;
import com.mifos.mifosxdroid.uihelpers.MFDatePicker;
import com.mifos.objects.group.Group;
import com.mifos.objects.organisation.Office;
import com.mifos.objects.group.GroupPayload;
import com.mifos.objects.organisation.Office;
import com.mifos.utils.DateHelper;
import com.mifos.utils.FragmentConstants;
import com.mifos.utils.MifosResponseHandler;
import com.mifos.utils.ValidationUtil;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -180,7 +181,7 @@ public void onClick(View view) {

private void initiateGroupCreation(GroupPayload groupPayload) {
//TextField validations
if (!isValidGroupName()) {
if (!isGroupNameValid()) {
return;
}

Expand Down Expand Up @@ -234,7 +235,8 @@ public void onDatePicked(String date) {

}

public boolean isValidGroupName() {
public boolean isGroupNameValid() {
result = true;
try {
if (TextUtils.isEmpty(et_groupName.getEditableText().toString())) {
throw new RequiredFieldException(getResources().getString(R.string.group_name),
Expand All @@ -245,7 +247,7 @@ public boolean isValidGroupName() {
.getEditableText().toString().trim().length() > 0) {
throw new ShortOfLengthException(getResources().getString(R.string.group_name), 4);
}
if (!et_groupName.getEditableText().toString().matches("[a-zA-Z]+")) {
if (!ValidationUtil.isNameValid(et_groupName.getEditableText().toString())) {
throw new InvalidTextInputException(getResources().getString(R.string.group_name)
, getResources().getString(R.string.error_should_contain_only),
InvalidTextInputException.TYPE_ALPHABETS);
Expand Down
11 changes: 11 additions & 0 deletions mifosng-android/src/main/java/com/mifos/utils/ValidationUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class ValidationUtil {
"([0-4]\\d|5[0-5]))\\.(\\d|[1-9]\\d|1\\d\\d|2([0-4]\\d|5[0-5]))\\." +
"(\\d|[1-9]\\d|1\\d\\d|2([0-4]\\d|5[0-5]))\\.(\\d|[1-9]\\d|1\\d\\d|2([0-4]\\d|5[0-5])" +
")$";
private static final String NAME_REGEX_PATTERN = "^[\\p{L} .'-]+$";
private static Pattern domainNamePattern = Pattern.compile(DOMAIN_NAME_REGEX_PATTERN);
private static Matcher domainNameMatcher;
private static Pattern ipAddressPattern = Pattern.compile(IP_ADDRESS_REGEX_PATTERN);
Expand Down Expand Up @@ -87,4 +88,14 @@ public static boolean isValidDomain(final String hex) {
//TODO MAKE SURE YOU UPDATE THE REGEX to check for ports in the URL
return false;
}

/**
* Validates the Name of Client, Group, Center etc.
*
* @param string Name
* @return Boolean
*/
public static boolean isNameValid(String string) {
return string.matches(NAME_REGEX_PATTERN);
}
}

0 comments on commit 7c8d991

Please sign in to comment.