-
Notifications
You must be signed in to change notification settings - Fork 460
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[NotEmpty] Add 'emptyText' attribute.
- Loading branch information
1 parent
545f63f
commit d0f70b6
Showing
4 changed files
with
29 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
} | ||
} |