Giter Site home page Giter Site logo

validatable-textinput-layout's Introduction

ValidatableTextInputLayout

Build Status JitPack

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">

  <EditText
    android: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.

private fun initValidator() {
  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.

Attribute Description
text_changed Validate immediately when the text is changed.
focus_changed Validate when the focus is changed

Validators

This library provides some common validators

Validator Description
RequiredValidator required_validator.gif
EmailValidator email_validator.gif
NumberOnlyValidator Number only
AsciiOnlyValidator ascii_validator.gif
AlphabetOnlyValidator Alphabet character only
HiraganaOnlyValidator Jananese Hieragana character only
KatakanaOnlyValidator Japanese 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.

class MaterialDesignColorsValidator(
        private val api: MaterialDesignColorsApi,
        private val context: Context
) : VtlValidator {

    override fun validateAsCompletable(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()))
                }
    }

    override fun validate(text: String?): Boolean {
        throw UnsupportedOperationException("Sync method is not arrowed because this validation uses async API response.")
    }

    override fun getErrorMessage(): 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.

validatable-textinput-layout's People

Contributors

konifar avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.