Skip to content

Commit

Permalink
fix input field validation when focus changes
Browse files Browse the repository at this point in the history
  • Loading branch information
lbalmaceda committed Oct 18, 2017
1 parent e1dc804 commit de551ae
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public class ValidatedInputView extends LinearLayout {
private ImageCheckbox showPasswordToggle;
private IdentityListener identityListener;
private int inputIcon;
private boolean hasValidInput;
private boolean hasValidInput = true;
private boolean allowShowPassword = true;
private View outline;
private ShapeDrawable focusedOutlineBackground;
Expand Down Expand Up @@ -148,7 +148,7 @@ private void init(AttributeSet attrs) {
focusedOutlineBackground = ViewUtils.getRoundedOutlineBackground(getResources(), ContextCompat.getColor(getContext(), R.color.com_auth0_lock_input_field_border_focused));
normalOutlineBackground = ViewUtils.getRoundedOutlineBackground(getResources(), ContextCompat.getColor(getContext(), R.color.com_auth0_lock_input_field_border_normal));
errorOutlineBackground = ViewUtils.getRoundedOutlineBackground(getResources(), ContextCompat.getColor(getContext(), R.color.com_auth0_lock_input_field_border_error));
updateBorder(true);
updateBorder();

setNextFocusRightId(R.id.com_auth0_lock_show_password_toggle);
showPasswordToggle.setOnCheckedChangeListener(new ImageCheckbox.OnCheckedChangeListener() {
Expand All @@ -173,7 +173,7 @@ public void onCheckedChanged(ImageButton view, boolean isChecked) {
input.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
updateBorder(true);
updateBorder();
}
});
}
Expand Down Expand Up @@ -225,7 +225,7 @@ private void notifyEmailChanged(String emailInput) {
private Runnable uiUpdater = new Runnable() {
@Override
public void run() {
updateBorder(hasValidInput);
updateBorder();
}
};

Expand Down Expand Up @@ -307,14 +307,12 @@ private void setupInputValidation() {

/**
* Updates the view knowing if the input is valid or not.
*
* @param isValid if the input is valid or not for this kind of DataType.
*/
@CallSuper
protected void updateBorder(boolean isValid) {
protected void updateBorder() {
boolean isFocused = input.hasFocus() && !input.isInTouchMode();
ViewUtils.setBackground(outline, isValid ? (isFocused ? focusedOutlineBackground : normalOutlineBackground) : errorOutlineBackground);
errorDescription.setVisibility(isValid ? GONE : VISIBLE);
ViewUtils.setBackground(outline, hasValidInput ? (isFocused ? focusedOutlineBackground : normalOutlineBackground) : errorOutlineBackground);
errorDescription.setVisibility(hasValidInput ? GONE : VISIBLE);
requestLayout();
}

Expand All @@ -333,7 +331,8 @@ private void createBackground() {
*/
public void setDataType(@DataType int type) {
dataType = type;
updateBorder(true);
hasValidInput = true;
updateBorder();
setupInputValidation();
}

Expand All @@ -348,15 +347,15 @@ protected int getDataType() {
}

/**
* Validates the input data and updates the icon. DataType must be set.
* Validates the input data and updates the border. DataType must be set.
* Empty fields are considered valid.
*
* @return whether the data is valid or not.
*/
public boolean validate() {
boolean isValid = validate(true);
updateBorder(isValid);
return isValid;
hasValidInput = validate(true);
updateBorder();
return hasValidInput;
}

/**
Expand Down Expand Up @@ -481,7 +480,8 @@ public void setIcon(@DrawableRes int icon) {
public void clearInput() {
Log.v(TAG, "Input cleared and validation errors removed");
input.setText("");
updateBorder(true);
hasValidInput = true;
updateBorder();
showPasswordToggle.setChecked(false);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
public class ValidatedPasswordInputView extends ValidatedInputView {
private static final String TAG = ValidatedPasswordInputView.class.getSimpleName();
private PasswordStrengthView strengthView;
private boolean hasValidInput = true;

public ValidatedPasswordInputView(Context context) {
super(context);
Expand All @@ -35,20 +36,16 @@ public void init() {
protected boolean validate(boolean validateEmptyFields) {
String value = getText();
//Run strength validation to update ui
final boolean valid = strengthView.isValid(value);
if (!validateEmptyFields && value.isEmpty()) {
return true;
}

Log.v(TAG, "Field validation results: Is valid? " + valid);
return valid;
hasValidInput = strengthView.isValid(value) || !validateEmptyFields && value.isEmpty();
Log.v(TAG, "Field validation results: Is valid? " + hasValidInput);
return hasValidInput;
}

@Override
protected void updateBorder(boolean isValid) {
super.updateBorder(isValid);
protected void updateBorder() {
super.updateBorder();
if (strengthView != null && strengthView.isEnabled()) {
strengthView.setVisibility(!isValid || !getText().isEmpty() ? VISIBLE : GONE);
strengthView.setVisibility(!hasValidInput || !getText().isEmpty() ? VISIBLE : GONE);
}
}

Expand Down

0 comments on commit de551ae

Please sign in to comment.