Skip to content

Repository files navigation

ValidatableTextInputLayout

Build StatusJitPack

ValidatableTextInputLayout is the view which extended TextInputLayout to validate the input text easily.

Download

Project build.gradle

allprojects {
repositories {
...
maven { url "https://jitpack.io" }
}
}

App build.gradle

dependencies {
...
compile 'com.github.Kyash:validatable-textinput-layout:LATEST_VERSION'
}

LATEST_VERSION is JitPack which supports AndroidX. If you still use Support Library, please use version 0.3.0.

Basic usage

You can use as same as TextInputLayout in layout xml.

Layout

trigger attribute defines the timing of the text field validation.

<co.kyash.vtl.ValidatableTextInputLayout
id="@id/first_name"android:layout_width="match_parent"android:layout_height="wrap_content"app:trigger="text_changed">
<EditTextandroid:layout_width="match_parent"android:layout_height="wrap_content"android:hint="@string/first_name"android:inputType="textPersonName" />
</co.kyash.vtl.ValidatableTextInputLayout>

Kotlin

Register Validator class to ValidatableTextInputLayout For example, RequiredValidator shows error when the field is empty.

privatefuninitValidator() {
binding.firstName.register(RequiredValidator(getString(R.string.validation_error_required)))
}

That's it. It works as below.

required_validator.gif

Triggers

There are 2 types of the validation trigger attributes.

AttributeDescription
text_changedValidate immediately when the text is changed.
focus_changedValidate when the focus is changed

Validators

This library provides some common validators

ValidatorDescription
RequiredValidatorrequired_validator.gif
EmailValidatoremail_validator.gif
NumberOnlyValidatorNumber only
AsciiOnlyValidatorascii_validator.gif
AlphabetOnlyValidatorAlphabet character only
HiraganaOnlyValidatorJananese Hieragana character only
KatakanaOnlyValidatorJapanese Katakana character only

Custom validator

You can create the custom validator by using VtlValidator. Since VtlValidator uses RxJava2, it can handle async logic like API as well!

MaterialDesignColorsValidator is example to get data via API and validate the input value.

classMaterialDesignColorsValidator(
privatevalapi:MaterialDesignColorsApi,
privatevalcontext:Context
) : VtlValidator {
overridefunvalidateAsCompletable(context:Context, text:String?): Completable {
return api.all()
.onErrorResumeNext { Single.error(VtlValidationFailureException(context.getString(R.string.validation_error_server))) }
.flatMapCompletable { list ->if (text?.trim() !=null) {
list.filter { it == text.trim().toLowerCase() }
.forEach { return@flatMapCompletable Completable.complete() }
}
return@flatMapCompletable Completable.error(VtlValidationFailureException(getErrorMessage()))
}
}
overridefunvalidate(text:String?): Boolean {
throwUnsupportedOperationException("Sync method is not arrowed because this validation uses async API response.")
}
overridefungetErrorMessage(): String {
return context.getString(R.string.validation_error_colors)
}
}

custom_validator.gif

Contributing

We are always welcome your contribution! If you find a bug or want to add new feature, please raise issue.

License

Copyright 2018 Kyash
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.

About

Android TextInputLayout with rx-validation feature

Topics

Resources

Stars

24 stars

Watchers

36 watching

Forks

Releases

Packages

Used by

Contributors

Languages