Skip to content

Instantly share code, notes, and snippets.

@faogustavo
Last active November 25, 2016 19:26
Show Gist options
  • Save faogustavo/77748c8816771f896fdb4bd345bb2600 to your computer and use it in GitHub Desktop.
Save faogustavo/77748c8816771f896fdb4bd345bb2600 to your computer and use it in GitHub Desktop.
MaskedEditText Saripaar Required
/*
* Copyright (C) 2016 Gustavo Fão Valvassori
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language governing permissions and
* limitations under the License.
*/
package br.com.syntesis.portal.Utils.validator.MaskedTextRule;
import com.mobsandgeeks.saripaar.ContextualAnnotationRule;
import com.mobsandgeeks.saripaar.ValidationContext;
public class MaskedEditTextRule extends ContextualAnnotationRule<MaskedRequire, String> {
protected MaskedEditTextRule(final ValidationContext validationContext, final MaskedRequire cpfCnpj) {
super(validationContext, cpfCnpj);
}
@Override
public boolean isValid(String text) {
boolean isValidAnnotation =
!mRuleAnnotation.mask().isEmpty() &&
!mRuleAnnotation.notMaskedSymbol().isEmpty();
if (isValidAnnotation) {
String emptyField = mRuleAnnotation.mask().replaceAll(
mRuleAnnotation.notMaskedSymbol(), mRuleAnnotation.replacementChar()
);
return !emptyField.equals(text);
}
return false;
}
}
/*
* Copyright (C) 2016 Gustavo Fão Valvassori
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language governing permissions and
* limitations under the License.
*/
package br.com.syntesis.portal.Utils.validator.MaskedTextRule;
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;
@ValidateUsing(MaskedEditTextRule.class)
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface MaskedRequire {
String mask() default "";
String notMaskedSymbol() default "\\*";
String replacementChar() default " ";
//Just because its required
int sequence() default -1;
String message() default "This field is required";
int messageResId() default -1;
}
@faogustavo
Copy link
Author

For use saripaar just add the @MaskedRequire passing the mask as param.
You can also change the notMaskedSymbol and the replacementChar to verify.

@MaskedRequire(mask = "() ****-***")
MaskedEditText phone;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment