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 d51309a
Showing 1 changed file with 178 additions and 50 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 All @@ -24,6 +26,7 @@
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnTextChanged;

/**
* Created by dilpreet on 31/7/17.
Expand Down Expand Up @@ -62,6 +65,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 @@ -81,11 +85,49 @@ public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
return rootView;
}

@OnTextChanged(R.id.et_account_number)
public void accountNumberTextChanged() {
validateAccountNumber();
}

@OnTextChanged(R.id.et_username)
public void usernameTextChanged() {
validateUsername();
}

@OnTextChanged(R.id.et_first_name)
public void firstNameTextChanged() {
validateFirstName();
}

@OnTextChanged(R.id.et_last_name)
public void lastNameTextChanged() {
validateLastName();
}

@OnTextChanged(R.id.et_phone_number)
public void phoneNumberTextChanged() {
validatePhoneNumber();
}

@OnTextChanged(R.id.et_email)
public void emailTextChanged() {
validateEmail();
}

@OnTextChanged(R.id.et_password)
public void passwordTextChanged() {
validatePassword();
}

@OnTextChanged(R.id.et_confirm_password)
public void confirmPasswordTextChanged() {
validateConfirmPassword();
}

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

if (areFieldsValidated()) {

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

Expand All @@ -96,12 +138,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 +151,153 @@ public void registerClicked() {
}

private boolean areFieldsValidated() {
fieldNotValidated = false;
validateAccountNumber();
validateUsername();
validateFirstName();
validateLastName();
validatePhoneNumber();
validateEmail();
validatePassword();
validateConfirmPassword();

return !fieldNotValidated;
}

if (etAccountNumber.getText().toString().trim().length() == 0) {
Toaster.show(rootView, getString(R.string.error_validation_blank, getString(R.string.
private void validateAccountNumber() {
if (TextUtils.isEmpty(getTrimmedInputText(etAccountNumber))) {
showInputErrorAndChangeValidationBoolean(etAccountNumber,
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.
} else {
hideInputError(etAccountNumber);
}
}

private void validateUsername() {
if (TextUtils.isEmpty(getTrimmedInputText(etUsername))) {
showInputErrorAndChangeValidationBoolean(etUsername,
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,
} 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)));
return false;
} else if (etFirstName.getText().length() == 0) {
Toaster.show(rootView, getString(R.string.error_validation_blank, getString(R.string.
} else {
hideInputError(etUsername);
}
}

private void validateFirstName() {
if (TextUtils.isEmpty(getTrimmedInputText(etFirstName))) {
showInputErrorAndChangeValidationBoolean(etFirstName,
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.
} else {
hideInputError(etFirstName);
}
}

private void validateLastName() {
if (TextUtils.isEmpty(getTrimmedInputText(etLastName))) {
showInputErrorAndChangeValidationBoolean(etLastName,
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.
} else {
hideInputError(etLastName);
}
}

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

private void validateEmail() {
if (TextUtils.isEmpty(getTrimmedInputText(etEmail))) {
showInputErrorAndChangeValidationBoolean(etEmail,
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.
} else if (!Patterns.EMAIL_ADDRESS.matcher(getTrimmedInputText(etEmail))
.matches()) {
showInputErrorAndChangeValidationBoolean(etEmail,
getString(R.string.error_invalid_email));
} else {
hideInputError(etEmail);
}
}

private void validatePassword() {
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)));
return false;
} else if (etPassword.getText().toString().trim().length()
< etPassword.getText().toString().length()) {
Toaster.show(rootView,
} 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);
}
}

private void validateConfirmPassword() {
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)));
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())
.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().
} 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)));
return false;
} 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);
}
}

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);
}

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

@Override
Expand Down

0 comments on commit d51309a

Please sign in to comment.