Closed LuizGadao closed 8 years ago
Assim:
package br.com.validation;
import com.mobsandgeeks.saripaar.annotation.ValidateUsing;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Created by Alberto on 02/11/2015.
*/
@ValidateUsing(CpfCnpjRule.class)
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface CpfCnpj {
boolean isRequired() default false;
String emptyText() default "";
int emptyTextResId() default -1;
int sequence() default -1;
int messageResId() default -1;
String message() default "Cpf/Cnpj inválido";
}
package br.com.validation;
import android.view.View;
import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.collect.Iterators;
import com.google.common.collect.Lists;
import com.google.common.primitives.Chars;
import com.mobsandgeeks.saripaar.ContextualAnnotationRule;
import com.mobsandgeeks.saripaar.ValidationContext;
import com.mobsandgeeks.saripaar.annotation.NotEmpty;
import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* Created by Alberto on 02/11/2015.
*/
public class CpfCnpjRule extends ContextualAnnotationRule<CpfCnpj, String> {
protected CpfCnpjRule(final ValidationContext validationContext, final CpfCnpj cpfCnpj) {
super(validationContext, cpfCnpj);
}
@Override
public boolean isValid(String data) {
boolean stringIsEmpty = data == null || data.isEmpty();
if (mRuleAnnotation.isRequired() && stringIsEmpty)
return false;
else if (!mRuleAnnotation.isRequired() && stringIsEmpty)
return true;
return IsValidCNPJ(data) || IsValidCPF(data);
}
private static boolean IsValidCNPJ(String cnpj) {
return IsValid(cnpj, new Integer[]{6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2}, 14);
}
private static boolean IsValidCPF(String cpf) {
return IsValid(cpf, new Integer[]{11, 10, 9, 8, 7, 6, 5, 4, 3, 2}, 11);
}
private static boolean IsValid(String cpfCnpj, Integer[] mult, int length) {
if (cpfCnpj.length() != length)
return false;
String cpnjWithoutCheckDigit = cpfCnpj.substring(0, length - 2);
List<Integer> ints = Arrays.asList(mult);
int sum = Sum(cpnjWithoutCheckDigit, ints.subList(1, ints.size()));
Integer rest = sum % 11;
rest = rest < 2 ? 0 : 11 - rest;
String digits = rest.toString();
cpnjWithoutCheckDigit += digits;
sum = Sum(cpnjWithoutCheckDigit, ints);
rest = sum % 11;
rest = rest < 2 ? 0 : 11 - rest;
digits += rest.toString();
return cpfCnpj.endsWith(digits);
}
private static int Sum(String cpnjWithoutCheckDigit, List<Integer> mult) {
int sum = 0;
List<Integer> ints = Lists.transform(Chars.asList(cpnjWithoutCheckDigit.toCharArray()), new Function<Character, Integer>() {
@Override
public Integer apply(Character input) {
return Integer.parseInt(input.toString());
}
});
for (int i = 0; i < ints.size(); i++) sum += ints.get(i) * mult.get(i);
return sum;
}
}
E ai você usa assim:
@CpfCnpj(messageResId = R.string.invalid_cpf_cnpj, isRequired = true)
@Bind(R.id.client_main_cpf_cnpj)
EditText mClientCpnjCpf;
Eu tinha resolvido isso.
Mas, de qualquer forma muito obrigado.
@AlbertoMonteiro Good job!
Tried doing this, but CpfCnpjRule is never getting called for EditTexts annotated with CpfCnpj. Did something change in Saripaar's API?
Did you register CpfCnpjRule
using validator.registerAnnotation(CpfCnpjRule.class)
?
@ragunathjawahar Ha! That did it! But I had to do it a little differently:
Validator.registerAnnotation(CpfCnpj.class);
Thanks! 🎊
@mcassiano You're right, that's a static method :)
How can I validate a field with this algorithm: