Skip to content

Commit

Permalink
Merge pull request #10 from julianraj/feature-validation-callback
Browse files Browse the repository at this point in the history
add support for callback on validation event
  • Loading branch information
julianraj authored Aug 23, 2017
2 parents eac35db + a5cffe8 commit 400f22f
Show file tree
Hide file tree
Showing 14 changed files with 157 additions and 13 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,19 +89,24 @@ validatedtextinputlayout in the latest version [You may want to update your xml
}
}

- **Validation Callback**
You can add callbacks to the validators from constructor or a setter using the
`ValidationCallback` class.


## Usage ##
- **Maven**

<dependency>
<groupId>com.julianraj</groupId>
<artifactId>validatedtextinputlayout</artifactId>
<version>0.1.0</version>
<version>0.3.0</version>
<type>pom</type>
</dependency>

- **Gradle**

compile 'com.julianraj:validatedtextinputlayout:0.1.0'
compile 'com.julianraj:validatedtextinputlayout:0.3.0'


- You can use and style it similar to **Android Design Library's** _TextInputLayout_
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.0'
classpath 'com.android.tools.build:gradle:2.3.3'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.3'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1'
// NOTE: Do not place your application dependencies here; they belong
Expand Down
2 changes: 1 addition & 1 deletion circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ machine:
dependencies:
pre:
- echo y | android update sdk --no-ui --all --filter tool,extra-android-m2repository,extra-android-support,extra-google-google_play_services,extra-google-m2repository,android-25
- echo y | android update sdk --no-ui --all --filter build-tools-25.0.2
- echo y | android update sdk --no-ui --all --filter build-tools-25.0.3

test:
override:
Expand Down
2 changes: 1 addition & 1 deletion sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apply plugin: 'com.android.application'

android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
buildToolsVersion "25.0.3"

defaultConfig {
applicationId "com.julianraj.validatedtextinputlayout"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import com.julianraj.validatedtextinputlayout.ValidatedTextInputLayout;
import com.julianraj.validatedtextinputlayout.validator.BaseValidator;
import com.julianraj.validatedtextinputlayout.validator.DependencyValidator;
import com.julianraj.validatedtextinputlayout.validator.ValidationCallback;

public class MainActivity extends AppCompatActivity {

Expand All @@ -19,6 +22,7 @@ public class MainActivity extends AppCompatActivity {
ValidatedTextInputLayout mPasswordInput;
ValidatedTextInputLayout mConfPasswordInput;
ValidatedTextInputLayout mEmailInput;
ValidatedTextInputLayout mCustomInput;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -46,6 +50,22 @@ public void onClick(View view) {
"password."));

mEmailInput = ((ValidatedTextInputLayout) findViewById(R.id.email));

mCustomInput = ((ValidatedTextInputLayout) findViewById(R.id.custom));
ValidationCallback mCallback = new ValidationCallback() {
@Override
public void onValidation(boolean status) {
if (!status)
Toast.makeText(MainActivity.this, "Your validation callback was called.",
Toast.LENGTH_SHORT).show();
}
};
mCustomInput.addValidator(new BaseValidator("Only accepts the word 'Valid'", mCallback) {
@Override
public boolean isValid(String pText) {
return pText.equalsIgnoreCase("valid");
}
});
}

private void submitForm() {
Expand All @@ -72,6 +92,7 @@ private boolean validateFields() {
if (!mPasswordInput.validate()) flag = false;
if (!mEmailInput.validate()) flag = false;
if (!mConfPasswordInput.validate()) flag = false;
if (!mCustomInput.validate()) flag = false;
return flag;
}

Expand Down
21 changes: 18 additions & 3 deletions sample/src/main/res/layout/content_main.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView
android:id="@+id/container"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:validation="http://schemas.android.com/apk/res-auto"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/activity_vertical_margin"
Expand Down Expand Up @@ -39,8 +39,8 @@
android:id="@+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
validation:autoTrim="true"
android:layout_marginTop="8dp"
validation:autoTrim="true"
validation:autoValidate="true"
validation:errorAlwaysEnabled="true"
validation:regex="^[a-z0-9._%+-]+@(?:[a-z0-9-]+[.])+[a-z]{2,}$"
Expand Down Expand Up @@ -85,11 +85,26 @@

</com.julianraj.validatedtextinputlayout.ValidatedTextInputLayout>

<com.julianraj.validatedtextinputlayout.ValidatedTextInputLayout
android:id="@+id/custom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/activity_vertical_margin">

<android.support.design.widget.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Must be word 'Valid'"
android:inputType="textPassword"
android:singleLine="true"/>

</com.julianraj.validatedtextinputlayout.ValidatedTextInputLayout>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:background="#eaeaea"
android:padding="10dp"
android:text="** Here confirmation-password-field has dependency validator associated with password-field with the dependency type 'TYPE_EQUAL' [i.e. password and confirmation-password must be equal]"/>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
6 changes: 3 additions & 3 deletions validatedtextinputlayout/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ ext {
siteUrl = 'https://github.com/julianraj/ValidatedTextInputLayout'
gitUrl = 'https://github.com/julianraj/ValidatedTextInputLayout.git'

libraryVersion = '0.2.0'
libraryVersion = '0.3.0'

developerId = 'julianraj'
developerName = 'Julian Raj Manandhar'
Expand All @@ -27,13 +27,13 @@ ext {

android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
buildToolsVersion "25.0.3"

defaultConfig {
minSdkVersion 9
targetSdkVersion 25
versionCode 1
versionName "0.2.0"
versionName "0.3.0"
}
buildTypes {
release {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,8 @@ public boolean isAutoTrimEnabled() {
public boolean validate() {
boolean status = true;
String text = getValue();
for (IValidator validator : mValidators) {
if (!validator.isValid(text)) {
for (BaseValidator validator : mValidators) {
if (!validator.validate(text)) {
setErrorEnabled(true);
setError(validator.getErrorMessage());
status = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public abstract class BaseValidator implements IValidator {
*/
protected String mErrorMessage;

protected ValidationCallback mCallback;

/**
* Sole constructor. (For invocation by subclass constructors)
*
Expand All @@ -24,6 +26,27 @@ public BaseValidator(@NonNull String pErrorMessage) {
setErrorMessage(pErrorMessage);
}

public BaseValidator(@NonNull String errorMessage, ValidationCallback callback) {
mErrorMessage = errorMessage;
mCallback = callback;
}

/**
* Validate the associated {@link ValidatedTextInputLayout}.
* Also call the callback method if {@link ValidationCallback} is provided
*
* @param pText value associated with the input field
* @return validity of the field
*/
public boolean validate(String pText) {
boolean status = isValid(pText);

if (mCallback != null)
mCallback.onValidation(status);

return status;
}

/**
* Check if the associated {@link ValidatedTextInputLayout} is valid or not.
*
Expand Down Expand Up @@ -52,4 +75,8 @@ public void setErrorMessage(@NonNull String pErrorMessage) {
public String getErrorMessage() {
return mErrorMessage;
}

public void setCallback(ValidationCallback callback) {
mCallback = callback;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,20 @@ public DependencyValidator(@NonNull ValidatedTextInputLayout pDependsOn, @Depend
mDependencyType = pDependencyType;
}

/**
* @param pDependsOn field for dependency
* @param pDependencyType type of dependency between the fields (must be one of #TYPE_EQUAL
* or TYPE_REQUIRED_IF_EXISTS)
* @param pErrorMessage error message to display if validation fails
* @param pCallback callback for validation event
*/
public DependencyValidator(@NonNull ValidatedTextInputLayout pDependsOn, @DependencyType int
pDependencyType, @NonNull String pErrorMessage, ValidationCallback pCallback) {
super(pErrorMessage, pCallback);
mDependstOn = pDependsOn;
mDependencyType = pDependencyType;
}

@Override
public boolean isValid(String pText) {
if (mDependencyType == TYPE_EQUAL) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,38 @@ public LengthValidator(int pMinimumLength, int pMaximumLength, @NonNull String p
mMaximumLength = pMaximumLength;
}

/**
* @param pErrorMessage error message to display if validation fails
* param pCallback callback for validation event
*/
public LengthValidator(@NonNull String pErrorMessage, ValidationCallback pCallback) {
super(pErrorMessage, pCallback);
}

/**
* @param pMaximumLength maximum length that the value of field can be
* @param pErrorMessage error message to display if validation fails
* @param pCallback callback for validation event
*/
public LengthValidator(int pMaximumLength, @NonNull String pErrorMessage,
ValidationCallback pCallback) {
super(pErrorMessage, pCallback);
mMaximumLength = pMaximumLength;
}

/**
* @param pMinimumLength minimum length that the value of field must be
* @param pMaximumLength maximum length that the value of field can be
* @param pErrorMessage error message to display if validation fails
* @param pCallback callback for validation event
*/
public LengthValidator(int pMinimumLength, int pMaximumLength, @NonNull String pErrorMessage,
ValidationCallback pCallback) {
super(pErrorMessage, pCallback);
mMinimumLength = pMinimumLength;
mMaximumLength = pMaximumLength;
}

/**
* Check if the associated {@link ValidatedTextInputLayout} meets the length constraint
* associated with it.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@ public RegexValidator(@NonNull String pRegex, @NonNull String pErrorMessage) {
mRegex = pRegex;
}

/**
* @param pRegex regular expression to check against
* @param pErrorMessage error message to display if validation fails
* @param pCallback callback for validation event
*/
public RegexValidator(@NonNull String pRegex, @NonNull String pErrorMessage,
ValidationCallback pCallback) {
super(pErrorMessage, pCallback);
mRegex = pRegex;
}

/**
* Check if the associated {@link ValidatedTextInputLayout} has correct value.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.julianraj.validatedtextinputlayout.validator;

import android.support.annotation.NonNull;

import com.julianraj.validatedtextinputlayout.ValidatedTextInputLayout;

/**
Expand All @@ -15,6 +17,14 @@ public RequiredValidator(String pErrorMessage) {
super(pErrorMessage);
}

/**
* @param pErrorMessage error message to display if validation fails
* @param pCallback callback for validation event
*/
public RequiredValidator(@NonNull String pErrorMessage, ValidationCallback pCallback) {
super(pErrorMessage, pCallback);
}

/**
* Check if the associated {@link ValidatedTextInputLayout} is empty or not.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.julianraj.validatedtextinputlayout.validator;

/**
* Interface to implement for callback for validation events
*/

public interface ValidationCallback {
void onValidation(boolean status);
}

0 comments on commit 400f22f

Please sign in to comment.