|
1 | | -# IlhaSoft Support Validation Library |
| 1 | +# Data Binding Validator by Ilhasoft |
2 | 2 |
|
3 | | -[](https://jitpack.io/#org.bitbucket.ilhasoft/support-validation) |
| 3 | +[](https://jitpack.io/#Ilhasoft/data-binding-validator) |
4 | 4 |
|
5 | | -Biblioteca com objetivo de facilitar e agilizar a criação de formulários. |
| 5 | +The Data Binding Validator makes it easy and quick to validate fields in forms using data binding framework. |
6 | 6 |
|
7 | | -## Recursos disponíveis: |
| 7 | +## Download |
8 | 8 |
|
9 | | -* Configuração tamanho mínimo/máximo para campos de texto; |
10 | | -* Validar inputs baseado no tipo de dados utilizado (email, cartão de crédito, URL e etc); |
11 | | -* Mensagens pré-definidas traduzidas em vários idiomas; |
12 | | -* Suporte à [`TextInputLayout`](https://developer.android.com/reference/android/support/design/widget/TextInputLayout.html); |
| 9 | +Step 1: Add it in your root build.gradle at the end of repositories: |
13 | 10 |
|
14 | | -## Exemplo de utilização |
| 11 | +``` |
| 12 | +allprojects { |
| 13 | + repositories { |
| 14 | + ... |
| 15 | + maven { url 'https://jitpack.io' } |
| 16 | + } |
| 17 | +} |
| 18 | +``` |
15 | 19 |
|
16 | | - |
| 20 | +Step 2: Add the dependency |
| 21 | +``` |
| 22 | + dependencies { |
| 23 | + compile 'com.github.Ilhasoft:data-binding-validator:LATEST-VERSION' |
| 24 | + } |
| 25 | +``` |
| 26 | +Latest Version: [](https://jitpack.io/#Ilhasoft/data-binding-validator) |
17 | 27 |
|
18 | | -## Como utilizar? |
| 28 | + |
| 29 | +## Features: |
19 | 30 |
|
20 | | -[Documentação oficial](https://bitbucket.org/ilhasoft/support-validation/wiki/Como%20utilizar) |
| 31 | +* Minimum/Maximum length validation for text fields; |
| 32 | +* Validate inputs based on field type (email, credit card, URL, CPF and so on); |
| 33 | +* Pre-defined error messages translated into English, Portuguese and Spanish; |
| 34 | +* Custom error messages by field; |
| 35 | +* Supports [`TextInputLayout`](https://developer.android.com/reference/android/support/design/widget/TextInputLayout.html) and EditText; |
| 36 | + |
| 37 | +## Sample |
| 38 | + |
| 39 | +<img width='380' src='https://raw.githubusercontent.com/Ilhasoft/data-binding-validator/master/screenshot.png'/> |
| 40 | + |
| 41 | +## Usage |
| 42 | + |
| 43 | +### Enabling Data Binding ### |
| 44 | + |
| 45 | +You need to enable Data Binding to use this library, add the following code into your main module's `build.gradle`: |
| 46 | + |
| 47 | +``` |
| 48 | +android { |
| 49 | + .... |
| 50 | + dataBinding { |
| 51 | + enabled = true |
| 52 | + } |
| 53 | +} |
| 54 | +``` |
| 55 | + |
| 56 | +### Setting up validations directly on layout ### |
| 57 | + |
| 58 | +It's possible to insert directly on layout creation, the validation on input fields. The error messages in different languages already are configured inside the library, not requiring the adding by developers. These are the existing validation types: |
| 59 | + |
| 60 | +#### Validate Characters Length #### |
| 61 | + |
| 62 | +Adding `validateMinLength` or `validateMaxLength` to your `EditText`, it's possible to configure a minimum or maximum characters length: |
| 63 | + |
| 64 | +``` |
| 65 | +<EditText |
| 66 | + android:id="@+id/name" |
| 67 | + android:layout_width="match_parent" |
| 68 | + android:layout_height="wrap_content" |
| 69 | + android:hint="Name" |
| 70 | + app:validateMinLength="@{4}" |
| 71 | + app:validateMaxLength="@{10}"/> |
| 72 | +``` |
| 73 | + |
| 74 | +#### Validate Empty Characters #### |
| 75 | + |
| 76 | +Adding `validateEmpty`, you can validate if the `EditText` is empty: |
| 77 | + |
| 78 | +``` |
| 79 | +<EditText |
| 80 | + android:id="@+id/hello" |
| 81 | + android:layout_width="match_parent" |
| 82 | + android:layout_height="wrap_content" |
| 83 | + android:hint="Name" |
| 84 | + app:validateEmpty="@{true}" /> |
| 85 | +``` |
| 86 | + |
| 87 | +#### Validate Date Patterns #### |
| 88 | + |
| 89 | +Adding `validateDate`, you can set a pattern accepted by the `EditText` such as `dd/MM/yyyy`, `yyyy` and so on: |
| 90 | + |
| 91 | +``` |
| 92 | +<EditText |
| 93 | + android:id="@+id/date" |
| 94 | + android:layout_width="match_parent" |
| 95 | + android:layout_height="wrap_content" |
| 96 | + android:hint="Name" |
| 97 | + app:validateDate='@{"dd/MM/yyyy"}' /> |
| 98 | +``` |
| 99 | + |
| 100 | +#### Validate Regex #### |
| 101 | + |
| 102 | +Adding `validateRegex`, you can set a regular expression to be validated, for example: |
| 103 | + |
| 104 | +``` |
| 105 | +<EditText |
| 106 | + android:id="@+id/regex" |
| 107 | + android:layout_width="match_parent" |
| 108 | + android:layout_height="wrap_content" |
| 109 | + android:hint="Name" |
| 110 | + app:validateRegex='@{"[a-zA-Z0-9-._]+"}' |
| 111 | + app:validateRegexMessage="@{@string/regexErrorMessage}" /> |
| 112 | +``` |
| 113 | + |
| 114 | +#### Validate Input Types #### |
| 115 | + |
| 116 | +You can even validate input by date, for example Email, URL, Username, CreditCard, CPF, CEP and so on: |
| 117 | + |
| 118 | +``` |
| 119 | +<EditText app:validateType='@{"email"}' /> |
| 120 | +
|
| 121 | +<EditText app:validateType='@{"url"}' /> |
| 122 | +
|
| 123 | +<EditText app:validateType='@{"creditCard"}' /> |
| 124 | +
|
| 125 | +<EditText app:validateType='@{"username"}' /> |
| 126 | +
|
| 127 | +<EditText app:validateType='@{"cpf"}' /> |
| 128 | +``` |
| 129 | + |
| 130 | +### Applying Validation ### |
| 131 | + |
| 132 | +It will be necessary to instantiate `Validator` passing as argument your `ViewDataBinding` instance got from your layout binding. After that you can call `validate()` that will return if your data is valid or not. Example: |
| 133 | + |
| 134 | +``` |
| 135 | +@Override |
| 136 | +protected void onCreate(Bundle savedInstanceState) { |
| 137 | + super.onCreate(savedInstanceState); |
| 138 | +
|
| 139 | + MainActivityBinding binding = DataBindingUtil.setContentView(this, R.layout.main_activity); |
| 140 | + final Validator validator = new Validator(binding); |
| 141 | +
|
| 142 | + binding.validate.setOnClickListener(new View.OnClickListener() { |
| 143 | + @Override |
| 144 | + public void onClick(View v) { |
| 145 | + if (validator.validate()) { |
| 146 | + saveToDatabase(); |
| 147 | + } |
| 148 | + } |
| 149 | + }); |
| 150 | +} |
| 151 | +``` |
| 152 | + |
| 153 | +### Custom Error Messages ### |
| 154 | + |
| 155 | +You can add custom error messages by using the same validation rule name and adding `Message` at the end, such as `validateTypeMessage`, `validateDateMessage`, `validateRegexMessage` and so on. For example: |
| 156 | + |
| 157 | +``` |
| 158 | +<EditText |
| 159 | + android:id="@+id/date" |
| 160 | + android:layout_width="match_parent" |
| 161 | + android:layout_height="wrap_content" |
| 162 | + android:hint="Name" |
| 163 | + app:validateDate='@{"dd/MM/yyyy"}' |
| 164 | + app:validateDateMessage="@{@string/dateErrorMessage}" /> |
| 165 | +``` |
| 166 | + |
| 167 | +### Validation modes ### |
| 168 | + |
| 169 | +The validation can be applied in two way, field by field or the whole form at once. By default, it's configured field by field, however, you can call `validator.enableFormValidationMode();` to enable the validation of the whole form. |
| 170 | + |
| 171 | +If you want to come back to the default way, call `validator.enableFieldValidationMode();` |
| 172 | + |
| 173 | +### Auto dismiss ### |
| 174 | + |
| 175 | +By default, the library auto dismiss the error messages when you start typing again, but it's configurable as well. You can add on your layout validation the same validation rule and adding `AutoDismiss` at the end, which receives a `boolean`. For example: |
| 176 | + |
| 177 | +``` |
| 178 | +<EditText |
| 179 | + android:id="@+id/date" |
| 180 | + android:layout_width="match_parent" |
| 181 | + android:layout_height="wrap_content" |
| 182 | + android:hint="Name" |
| 183 | + app:validateDate='@{"dd/MM/yyyy"}' |
| 184 | + app:validateDateMessage="@{@string/dateErrorMessage}" |
| 185 | + app:validateDateAutoDismiss="@{false}" /> |
| 186 | +``` |
0 commit comments