Skip to content

Commit

Permalink
Feat: Add error message for all fields, add live update validation
Browse files Browse the repository at this point in the history
Add error message for all fields and live update validation, as same as
PayPal has.

Issue: #896
  • Loading branch information
letelete committed Oct 25, 2018
1 parent e6ef485 commit b6ecc5a
Showing 1 changed file with 123 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TextInputLayout;
import android.text.TextUtils;
import android.util.Patterns;
import android.view.LayoutInflater;
import android.view.View;
Expand Down Expand Up @@ -62,6 +64,7 @@ public class RegistrationFragment extends BaseFragment implements RegistrationVi
RegistrationPresenter presenter;

private View rootView;
private boolean fieldNotValidated;

public static RegistrationFragment newInstance() {
RegistrationFragment fragment = new RegistrationFragment();
Expand All @@ -83,9 +86,7 @@ public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,

@OnClick(R.id.btn_register)
public void registerClicked() {

if (areFieldsValidated()) {

RadioButton radioButton = rootView.findViewById(rgVerificationMode.
getCheckedRadioButtonId());

Expand All @@ -96,12 +97,6 @@ public void registerClicked() {
payload.setFirstName(etFirstName.getText().toString());
payload.setLastName(etLastName.getText().toString());
payload.setMobileNumber(etPhoneNumber.getText().toString());
if (!etPassword.getText().toString().equals(etConfirmPassword.getText().toString())) {
Toaster.show(rootView, getString(R.string.error_password_not_match));
return;
} else {
payload.setPassword(etPassword.getText().toString());
}
payload.setPassword(etPassword.getText().toString());
payload.setUsername(etUsername.getText().toString().replace(" ", ""));

Expand All @@ -115,61 +110,130 @@ public void registerClicked() {
}

private boolean areFieldsValidated() {
fieldNotValidated = false;

if (TextUtils.isEmpty(getTrimmedInputText(etAccountNumber))) {
showInputErrorAndChangeValidationBoolean(etAccountNumber,
getString(R.string.error_validation_blank, getString(R.string.
account_number)));
} else {
hideInputError(etAccountNumber);
}

if (etAccountNumber.getText().toString().trim().length() == 0) {
Toaster.show(rootView, getString(R.string.error_validation_blank, getString(R.string.
account_number)));
return false;
} else if (etUsername.getText().toString().trim().length() == 0) {
Toaster.show(rootView, getString(R.string.error_validation_blank, getString(R.string.
username)));
return false;
} else if (etUsername.getText().toString().trim().length() < 5) {
Toaster.show(rootView, getString(R.string.error_validation_minimum_chars,
getString(R.string.username),
getResources().getInteger(R.integer.username_minimum_length)));
return false;
} else if (etUsername.getText().toString().trim().contains(" ")) {
Toaster.show(rootView, getString(R.string.error_validation_cannot_contain_spaces,
getString(R.string.username), getString(R.string.not_contain_username)));
return false;
} else if (etFirstName.getText().length() == 0) {
Toaster.show(rootView, getString(R.string.error_validation_blank, getString(R.string.
first_name)));
return false;
} else if (etLastName.getText().toString().trim().length() == 0) {
Toaster.show(rootView, getString(R.string.error_validation_blank, getString(R.string.
last_name)));
return false;
} else if (etEmail.getText().toString().trim().length() == 0) {
Toaster.show(rootView, getString(R.string.error_validation_blank, getString(R.string.
email)));
return false;
} else if (etPassword.getText().toString().trim().length() == 0) {
Toaster.show(rootView, getString(R.string.error_validation_blank, getString(R.string.
password)));
return false;
} else if (etPassword.getText().toString().trim().length()
< etPassword.getText().toString().length()) {
Toaster.show(rootView,
getString(R.string.error_validation_cannot_contain_leading_or_trailing_spaces,
getString(R.string.password)));
return false;
} else if (etUsername.getText().toString().trim().length() < 6) {
Toaster.show(rootView, getString(R.string.error_username_greater_than_six));
return false;
} else if (!Patterns.EMAIL_ADDRESS.matcher( etEmail.getText().toString().trim())
if (TextUtils.isEmpty(getTrimmedInputText(etUsername))) {
showInputErrorAndChangeValidationBoolean(etUsername,
getString(R.string.error_validation_blank, getString(R.string.
username)));
} else if (getTrimmedInputText(etUsername).length() < 6) {
showInputErrorAndChangeValidationBoolean(etUsername,
getString(R.string.error_username_greater_than_six));
} else if (getTrimmedInputText(etUsername).contains(" ")) {
showInputErrorAndChangeValidationBoolean(etUsername,
getString(R.string.error_validation_cannot_contain_spaces,
getString(R.string.username),
getString(R.string.not_contain_username)));
} else {
hideInputError(etUsername);
}

if (TextUtils.isEmpty(getTrimmedInputText(etFirstName))) {
showInputErrorAndChangeValidationBoolean(etFirstName,
getString(R.string.error_validation_blank, getString(R.string.
first_name)));
} else {
hideInputError(etFirstName);
}

if (TextUtils.isEmpty(getTrimmedInputText(etLastName))) {
showInputErrorAndChangeValidationBoolean(etLastName,
getString(R.string.error_validation_blank, getString(R.string.
last_name)));
} else {
hideInputError(etLastName);
}

if (TextUtils.isEmpty(getTrimmedInputText(etPhoneNumber))) {
showInputErrorAndChangeValidationBoolean(etPhoneNumber,
getString(R.string.error_validation_blank, getString(R.string.
phone_number)));
} else {
hideInputError(etPhoneNumber);
}

if (TextUtils.isEmpty(getTrimmedInputText(etEmail))) {
showInputErrorAndChangeValidationBoolean(etEmail,
getString(R.string.error_validation_blank, getString(R.string.
email)));
} else if (!Patterns.EMAIL_ADDRESS.matcher(getTrimmedInputText(etEmail))
.matches()) {
Toaster.show(rootView, getString(R.string.error_invalid_email));
return false;
} else if (etPassword.getText().toString().trim().length() < 6) {
Toaster.show(rootView, getString(R.string.error_validation_minimum_chars,
getString(R.string.password), getResources().
getInteger(R.integer.password_minimum_length)));
return false;
showInputErrorAndChangeValidationBoolean(etEmail,
getString(R.string.error_invalid_email));
} else {
hideInputError(etEmail);
}

return true;
if (getTrimmedInputText(etPassword).length() < etPassword.getText().toString().length()) {
showInputErrorAndChangeValidationBoolean(etPassword,
getString(R.string.error_validation_cannot_contain_leading_or_trailing_spaces,
getString(R.string.password)));
} else if (TextUtils.isEmpty(getTrimmedInputText(etPassword))) {
showInputErrorAndChangeValidationBoolean(etPassword,
getString(R.string.error_validation_blank, getString(R.string.
password)));
} else if (getTrimmedInputText(etPassword).length() < 6) {
showInputErrorAndChangeValidationBoolean(etPassword,
getString(R.string.error_validation_minimum_chars,
getString(R.string.password), getResources().
getInteger(R.integer.password_minimum_length)));
} else {
hideInputError(etPassword);
}

if (getTrimmedInputText(etConfirmPassword).length()
< etConfirmPassword.getText().toString().length()) {
showInputErrorAndChangeValidationBoolean(etConfirmPassword,
getString(R.string.error_validation_cannot_contain_leading_or_trailing_spaces,
getString(R.string.password)));
} else if (TextUtils.isEmpty(getTrimmedInputText(etConfirmPassword))) {
showInputErrorAndChangeValidationBoolean(etConfirmPassword,
getString(R.string.error_validation_blank, getString(R.string.
password)));
} else if (getTrimmedInputText(etConfirmPassword).length() < 6) {
showInputErrorAndChangeValidationBoolean(etConfirmPassword,
getString(R.string.error_validation_minimum_chars,
getString(R.string.password), getResources().
getInteger(R.integer.password_minimum_length)));
} else if (!etPassword.getText().toString()
.equals(etConfirmPassword.getText().toString())) {
showInputErrorAndChangeValidationBoolean(etConfirmPassword,
getString(R.string.error_validation_cannot_contain_leading_or_trailing_spaces,
getString(R.string.password)));
showInputErrorAndChangeValidationBoolean(etConfirmPassword,
getString(R.string.error_password_not_match));
} else {
hideInputError(etConfirmPassword);
}

return !fieldNotValidated;
}

private String getTrimmedInputText(EditText input) {
return input.getText().toString().trim();
}

private void showInputErrorAndChangeValidationBoolean(EditText input, String errorMsg) {
fieldNotValidated = true;
TextInputLayout textInputLayout = getInputLayoutByGivenEditText(input);
textInputLayout.setError(errorMsg);
}

private void hideInputError(EditText input) {
TextInputLayout textInputLayout = getInputLayoutByGivenEditText(input);
textInputLayout.setError(null);
}

private TextInputLayout getInputLayoutByGivenEditText(EditText input) {
return (TextInputLayout) input.getParent().getParent();
}

@Override
Expand Down

0 comments on commit b6ecc5a

Please sign in to comment.