Skip to content

Commit

Permalink
[NotEmpty] Add 'emptyText' attribute.
Browse files Browse the repository at this point in the history
  • Loading branch information
ragunathjawahar committed Sep 18, 2015
1 parent 545f63f commit d0f70b6
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ In your `{project_base}/build.gradle` file, include the following.

allprojects {
repositories {
mavenCentral()
jcenter()
maven {
url "https://oss.sonatype.org/content/repositories/snapshots/"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public abstract class ContextualAnnotationRule<RULE_ANNOTATION extends Annotatio
* @param ruleAnnotation The rule {@link java.lang.annotation.Annotation} instance to which
* this rule is paired.
*/
protected ContextualAnnotationRule(ValidationContext validationContext,
RULE_ANNOTATION ruleAnnotation) {
protected ContextualAnnotationRule(final ValidationContext validationContext,
final RULE_ANNOTATION ruleAnnotation) {
super(ruleAnnotation);
if (validationContext == null) {
throw new IllegalArgumentException("'validationContext' cannot be null.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface NotEmpty {
boolean trim() default false;
boolean trim() default false;
String emptyText() default "";
int emptyTextResId() default -1;

int sequence() default -1;
int messageResId() default -1;
String message() default "This field is required";
int sequence() default -1;
int messageResId() default -1;
String message() default "This field is required";
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,36 @@

package com.mobsandgeeks.saripaar.rule;

import com.mobsandgeeks.saripaar.AnnotationRule;
import android.content.Context;

import com.mobsandgeeks.saripaar.ContextualAnnotationRule;
import com.mobsandgeeks.saripaar.ValidationContext;
import com.mobsandgeeks.saripaar.annotation.NotEmpty;

/**
* @author Ragunath Jawahar {@literal <[email protected]>}
* @since 2.0
*/
public class NotEmptyRule extends AnnotationRule<NotEmpty, String> {
public class NotEmptyRule extends ContextualAnnotationRule<NotEmpty, String> {

protected NotEmptyRule(final NotEmpty notEmpty) {
super(notEmpty);
protected NotEmptyRule(final ValidationContext validationContext, final NotEmpty notEmpty) {
super(validationContext, notEmpty);
}

@Override
public boolean isValid(final String data) {
return data != null && (mRuleAnnotation.trim()
? data.trim().length() > 0 : data.length() > 0);
boolean isEmpty = false;
if (data != null) {
String text = mRuleAnnotation.trim() ? data.trim() : data;

Context context = mValidationContext.getContext();
String emptyText = mRuleAnnotation.emptyTextResId() != -1
? context.getString(mRuleAnnotation.emptyTextResId())
: mRuleAnnotation.emptyText();

isEmpty = emptyText.equals(text) || "".equals(text);
}

return !isEmpty;
}
}

0 comments on commit d0f70b6

Please sign in to comment.