Skip to content

Commit

Permalink
Implementado a validação no enveto de focus dos campos
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelcarvalho-q committed Jun 22, 2016
1 parent e01bcdb commit 16a5739
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 6 deletions.
19 changes: 15 additions & 4 deletions formvalidator/src/main/java/com/rca/formvalidator/Form.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import android.support.design.widget.TextInputLayout;

import com.rca.formvalidator.interfaces.FormViewInterface;

import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -47,15 +49,24 @@ public boolean validate() {
return isValid;
}

public void closeError(TextInputLayout textInputLayout) {
public void closeError(Validate validate) {

if (textInputLayout != null) {
if (validate != null) {

textInputLayout.setErrorEnabled(false);
textInputLayout.setError(null);
validate.closeError();
}
}

public void closeError(TextInputLayout textInputLayout) {

Validate.closeError(textInputLayout);
}

public void closeError(FormViewInterface formViewInterface) {

Validate.closeError(formViewInterface);
}

public void closeAllErrors() {

for (Validate validate : validateList) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,20 @@ public boolean isValid() {
@Override
public void closeError() {

closeError(textInputLayout);
closeError(formViewInterface);
}

public static void closeError(TextInputLayout textInputLayout) {

if (textInputLayout != null) {

textInputLayout.setErrorEnabled(false);
textInputLayout.setError(null);
}
}

public static void closeError(FormViewInterface formViewInterface) {

if (formViewInterface != null) {

Expand Down
6 changes: 4 additions & 2 deletions sample/src/main/java/com/rca/sample/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package com.rca.sample;

import android.os.Bundle;
import android.support.design.widget.TextInputLayout;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
Expand Down Expand Up @@ -107,13 +106,16 @@ private void setValidation() {
// Validates
Validate nameValidate = new Validate(etName, ilContentName);
nameValidate.addValidator(new NotEmptyValidator(this, R.string.err_empty));
etName.setOnFocusChangeListener(new OnFocusValidationListener(nameValidate));

Validate surNameValidate = new Validate(etSurname, fvContetSurname);
surNameValidate.addValidator(new NotEmptyValidator(this, R.string.err_empty));
etSurname.setOnFocusChangeListener(new OnFocusValidationListener(surNameValidate));

Validate emailValidate = new Validate(etEmail, ilContentEmail);
emailValidate.addValidator(new NotEmptyValidator(this, R.string.err_empty));
emailValidate.addValidator(new EmailValidator(this, R.string.err_email_invalid_format));
etEmail.setOnFocusChangeListener(new OnFocusValidationListener(emailValidate));

Validate checkBoxValidate = new Validate(cbExample, fvContentExample);
checkBoxValidate.addValidator(new CheckBoxValidator(this, R.string.required_field));
Expand Down
32 changes: 32 additions & 0 deletions sample/src/main/java/com/rca/sample/OnFocusValidationListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.rca.sample;

import android.support.annotation.NonNull;
import android.view.View;

import com.rca.formvalidator.Validate;

/**
* Created by rafael on 22/06/16.
*/
public class OnFocusValidationListener implements View.OnFocusChangeListener {

private Validate validate;

public OnFocusValidationListener(@NonNull Validate validate) {

this.validate = validate;
}

@Override
public void onFocusChange(View view, boolean hasFocus) {

if (hasFocus) {

validate.closeError();

} else {

validate.isValid();
}
}
}

0 comments on commit 16a5739

Please sign in to comment.