Skip to content

Commit

Permalink
Added IP4 validator
Browse files Browse the repository at this point in the history
  • Loading branch information
phajduk committed Jun 12, 2016
1 parent f7d7ea4 commit 94fdca4
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ At the moment we have following pre-defined validation rules:
- `maxLength`
- `age`
- `sameAs`
- `ip4`

You can also provide custom validation rule using `with(Validator<EditText> externalValidator)` operator. **It gives you possibility to define your own rules e.g. validate e-mail via HTTP call.**

Expand Down
11 changes: 11 additions & 0 deletions lib/src/main/java/com/github/phajduk/rxvalidator/RxValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.github.phajduk.rxvalidator.validators.DigitValidator;
import com.github.phajduk.rxvalidator.validators.EmailValidator;
import com.github.phajduk.rxvalidator.validators.InListValidator;
import com.github.phajduk.rxvalidator.validators.Ip4Validator;
import com.github.phajduk.rxvalidator.validators.LengthValidator;
import com.github.phajduk.rxvalidator.validators.MaxLengthValidator;
import com.github.phajduk.rxvalidator.validators.MinLengthValidator;
Expand Down Expand Up @@ -84,6 +85,16 @@ public RxValidator email(String invalidEmailMessage, Pattern pattern) {
return this;
}

public RxValidator ip4() {
this.validators.add(new Ip4Validator());
return this;
}

public RxValidator ip4(String invalidIp4Message) {
this.validators.add(new Ip4Validator(invalidIp4Message));
return this;
}

public RxValidator pattern(String invalidValueMessage, Pattern pattern) {
this.validators.add(new PatternValidator(invalidValueMessage, pattern));
return this;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.github.phajduk.rxvalidator.validators;

import java.util.regex.Pattern;

public class Ip4Validator extends PatternValidator {

private static final String DEFAULT_MESSAGE = "Invalid IP";

public Ip4Validator() {
super(DEFAULT_MESSAGE, android.util.Patterns.IP_ADDRESS);
}

public Ip4Validator(String invalidIpMessage) {
super(invalidIpMessage, android.util.Patterns.IP_ADDRESS);
}

public Ip4Validator(String invalidIpMessage, Pattern pattern) {
super(invalidIpMessage, pattern);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class MainActivity extends AppCompatActivity {
EditText password = (EditText) findViewById(R.id.etPassword);
EditText confirmPassword = (EditText) findViewById(R.id.etConfirmPassword);
EditText birthday = (EditText) findViewById(R.id.etBirthday);
EditText ip4Address = (EditText) findViewById(R.id.etIp4);
setDatePickerListener(birthday);

RxValidator.createFor(email)
Expand Down Expand Up @@ -100,6 +101,22 @@ public class MainActivity extends AppCompatActivity {
Log.e(TAG, "Validation error", throwable);
}
});

RxValidator.createFor(ip4Address)
.ip4("Invalid IP4 format")
.onValueChanged()
.toObservable()
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1<RxValidationResult<EditText>>() {
@Override public void call(RxValidationResult<EditText> result) {
result.getItem().setError(result.isProper() ? null : result.getMessage());
Log.i(TAG, "Validation result " + result.toString());
}
}, new Action1<Throwable>() {
@Override public void call(Throwable throwable) {
Log.e(TAG, "Validation error", throwable);
}
});
}

private void setDatePickerListener(final EditText birthday) {
Expand Down
7 changes: 7 additions & 0 deletions sample/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@
android:hint="birthday"
/>

<EditText
android:id="@+id/etIp4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="IP4 address"
/>

<Button
android:id="@+id/button"
android:text="Validate!"
Expand Down

0 comments on commit 94fdca4

Please sign in to comment.