Skip to content

Commit

Permalink
add NotifyType.kt to rules and change example to form ui
Browse files Browse the repository at this point in the history
  • Loading branch information
Mustafa Yiğit committed Jun 5, 2022
1 parent 7514518 commit 1eb3a07
Show file tree
Hide file tree
Showing 11 changed files with 249 additions and 84 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ dependencies {
implementation project(path: ':validator')

implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.6.0'
implementation 'androidx.appcompat:appcompat:1.4.2'
implementation 'com.google.android.material:material:1.6.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
Expand Down
19 changes: 19 additions & 0 deletions app/src/main/java/com/mustafayigit/validatable/CustomRule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.mustafayigit.validatable

import com.mustafayigit.validator.base.BaseValidatableRule
import com.mustafayigit.validator.base.NotifyType


/**
* Created by Mustafa Yiğit on 05/06/2022
* mustafa.yt65@gmail.com
*/
class PasswordConfirmRule(
override val errorMessage: String,
override val notifyType: NotifyType,
val originalInputGetter: () -> String
) : BaseValidatableRule(
errorMessage = errorMessage,
notifyType = notifyType,
rule = { it == originalInputGetter.invoke() }
)
150 changes: 115 additions & 35 deletions app/src/main/java/com/mustafayigit/validatable/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package com.mustafayigit.validatable

import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.widget.doOnTextChanged
import com.mustafayigit.validatable.databinding.ActivityMainBinding
import com.mustafayigit.validator.ValidatableRule
import com.mustafayigit.validator.Validator
import com.mustafayigit.validator.base.NotifyType

class MainActivity : AppCompatActivity() {
private val mBinding: ActivityMainBinding by lazy {
Expand All @@ -17,52 +19,130 @@ class MainActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)
setContentView(mBinding.root)

/* Init Validator with Default Constructor */
val validator = Validator(
onValidation = { isValid, errorMessage ->
Log.d("Validator", "isValid: $isValid, errorMessage: $errorMessage")
mBinding.btnAction.isEnabled = isValid
val emailValidator = Validator.Builder()
.addRules(
ValidatableRule.Email("Enter valid mail address"),
ValidatableRule.Required(
"Email is required", NotifyType.ON_FOCUS_CHANGE,
),
ValidatableRule.Email(
"Enter valid mail address", NotifyType.ON_FOCUS_CHANGE,
),
ValidatableRule.Required(
"Email is required", NotifyType.ON_FORM_SUBMIT,
),
ValidatableRule.Email(
"Enter valid mail address", NotifyType.ON_FORM_SUBMIT,
),
)
.addCollector {
mBinding.txtInputEmail.editText?.doOnTextChanged { text, _, _, _ ->
it.input = text.toString()
}
mBinding.txtInputEmail.editText?.setOnFocusChangeListener { _, isFocused ->
it.isFocused = isFocused
}
}
)
validator.addRules(
ValidatableRule.Email("Enter valid mail address"),
ValidatableRule.Required("Input is required"),
)
mBinding.editText.doOnTextChanged { text, _, _, _ ->
validator.input = text.toString()
}
/* ------------------------------------------------- */
.onValidate { isValid, errorMessage, notifyType ->
Log.d(
"Validator",
"notifyType: $notifyType, isValid: $isValid, errorMessage: $errorMessage"
)
mBinding.txtInputEmail.error = errorMessage
}
.build()

/* Init Validator with Constructor */
val validatorWithConstructor = Validator(
ValidatableRule.Email("Enter valid mail address"),
ValidatableRule.Required("Input is required"),
) { isValid, errorMessage ->
Log.d("Validator", "isValid: $isValid, errorMessage: $errorMessage")
mBinding.btnAction.isEnabled = isValid
}
mBinding.editText.doOnTextChanged { text, _, _, _ ->
validatorWithConstructor.input = text.toString()
}
/* ------------------------------------------------- */
val passwordValidator = Validator.Builder()
.addRules(
ValidatableRule.Password("Enter valid password"),
PasswordConfirmRule(
errorMessage = "Passwords does not match",
notifyType = NotifyType.ON_VALUE_CHANGE,
originalInputGetter = { mBinding.txtInputPasswordConfirm.editText?.text.toString() },
),
ValidatableRule.Required(
"Password is required", NotifyType.ON_FOCUS_CHANGE,
),
ValidatableRule.Password(
"Enter valid password", NotifyType.ON_FOCUS_CHANGE,
),
ValidatableRule.Required(
"Password is required", NotifyType.ON_FORM_SUBMIT,
),
ValidatableRule.Password(
"Enter valid password", NotifyType.ON_FORM_SUBMIT,
),
)
.addCollector {
mBinding.txtInputPassword.editText?.doOnTextChanged { text, _, _, _ ->
it.input = text.toString()
}
mBinding.txtInputPassword.editText?.setOnFocusChangeListener { _, isFocused ->
it.isFocused = isFocused
}
}
.onValidate { isValid, errorMessage, notifyType ->
Log.d(
"Validator",
"notifyType: $notifyType, isValid: $isValid, errorMessage: $errorMessage"
)
mBinding.txtInputPassword.error = errorMessage
}
.build()

/* Init Validator with Builder */
val validatorWithBuilder = Validator.Builder()
val passwordConfirmValidator = Validator.Builder()
.addRules(
ValidatableRule.Email("Enter valid mail address"),
ValidatableRule.Required("Input is required"),
PasswordConfirmRule(
errorMessage = "Passwords does not match",
notifyType = NotifyType.ON_VALUE_CHANGE,
originalInputGetter = { mBinding.txtInputPassword.editText?.text.toString() },
),
PasswordConfirmRule(
errorMessage = "Passwords does not match",
notifyType = NotifyType.ON_FORM_SUBMIT,
originalInputGetter = { mBinding.txtInputPassword.editText?.text.toString() },
),
ValidatableRule.Required(
"Password Confirm is required", NotifyType.ON_FOCUS_CHANGE,
),
ValidatableRule.Required(
"Password Confirm is required", NotifyType.ON_FORM_SUBMIT,
),
)
.addCollector {
mBinding.editText.doOnTextChanged { text, _, _, _ ->
mBinding.txtInputPasswordConfirm.editText?.doOnTextChanged { text, _, _, _ ->
it.input = text.toString()
}
mBinding.txtInputPasswordConfirm.editText?.setOnFocusChangeListener { _, isFocused ->
it.isFocused = isFocused
}
}
.onValidate { isValid, errorMessage ->
Log.d("Validator", "isValid: $isValid, errorMessage: $errorMessage")
mBinding.btnAction.isEnabled = isValid
.onValidate { isValid, errorMessage, notifyType ->
Log.d(
"Validator",
"notifyType: $notifyType, isValid: $isValid, errorMessage: $errorMessage"
)
mBinding.txtInputPasswordConfirm.error = errorMessage
}
.build()
/* ------------------------------------------------- */

// set isFormSubmitted to true to validate on form submit
mBinding.btnRegister.setOnClickListener {
emailValidator.submitForm()
passwordValidator.submitForm()
passwordConfirmValidator.submitForm()

if (emailValidator.submitForm().not() || passwordValidator.submitForm().not()) {
Toast.makeText(this, "Form is not valid", Toast.LENGTH_SHORT).show()
return@setOnClickListener
}
Toast.makeText(
this,
"Success",
Toast.LENGTH_SHORT
).show()

}


}
Expand Down
53 changes: 44 additions & 9 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,57 @@
android:paddingHorizontal="24dp"
tools:context=".MainActivity">

<EditText
android:id="@+id/edit_text"
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/txt_input_email"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Email"
android:inputType="textPersonName"
android:textColor="@color/black"
android:textColorHint="#99000000" />
android:hint="Email">

<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/black" />

</com.google.android.material.textfield.TextInputLayout>

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/txt_input_password"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:hint="Password">

<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/black" />

</com.google.android.material.textfield.TextInputLayout>

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/txt_input_password_confirm"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:hint="Password Confirm">

<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/black" />

</com.google.android.material.textfield.TextInputLayout>

<Button
android:id="@+id/btn_action"
android:id="@+id/btn_register"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="Action"
android:text="Register"
android:textAllCaps="false"
app:cornerRadius="10dp" />
app:cornerRadius="12dp" />

</LinearLayout>
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.mustafayigit.validator

import com.mustafayigit.validator.base.BaseValidatableRule
import com.mustafayigit.validator.base.NotifyType

/**
* Created by Mustafa Yiğit on 26/05/2022
Expand All @@ -17,16 +18,16 @@ interface IValidatable {
// rule list
val rules: MutableList<BaseValidatableRule>

var onValidation: (Boolean, String?) -> Unit
var onValidation: (Boolean, String?, NotifyType) -> Unit

// add rule and result action
fun addRules(vararg rules: BaseValidatableRule) {
this.rules.addAll(rules)
}

fun isValid(): Boolean
fun isValid(notifyType: NotifyType = NotifyType.ON_VALUE_CHANGE): Boolean

fun validate(){
isValid()
fun validate(notifyType: NotifyType): Boolean {
return isValid(notifyType)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.mustafayigit.validator

import com.mustafayigit.validator.base.BaseValidatableRule
import com.mustafayigit.validator.base.NotifyType
import com.mustafayigit.validator.util.Constants


Expand All @@ -11,34 +12,41 @@ import com.mustafayigit.validator.util.Constants

sealed class ValidatableRule(
override val errorMessage: String,
override val notifyType: NotifyType = NotifyType.ON_VALUE_CHANGE,
override val rule: (input: String) -> Boolean
) : BaseValidatableRule(errorMessage, rule) {
) : BaseValidatableRule(errorMessage, notifyType, rule) {

class Email(
override val errorMessage: String
override val errorMessage: String,
override val notifyType: NotifyType = NotifyType.ON_VALUE_CHANGE,
) : ValidatableRule(
errorMessage = errorMessage,
rule = { input -> Constants.REGEX_EMAIL.matches(input) }
)

class Password(
override val errorMessage: String
override val errorMessage: String,
override val notifyType: NotifyType = NotifyType.ON_VALUE_CHANGE,
) : ValidatableRule(
errorMessage = errorMessage,
rule = { input -> Constants.REGEX_PASSWORD.matches(input) }
rule = { input ->
input.length >= 6 && input.any { it.isUpperCase() } && input.any { it.isDigit() } && input.any { it.isLowerCase() }
}
)

class CharacterLength(
private val minCharacter: Int = 0,
private val maxCharacter: Int = 30,
override val errorMessage: String
override val errorMessage: String,
override val notifyType: NotifyType = NotifyType.ON_FOCUS_CHANGE,
) : ValidatableRule(
errorMessage = errorMessage,
rule = { input -> input.length in minCharacter..maxCharacter }
)

class Required(
override val errorMessage: String
override val errorMessage: String,
override val notifyType: NotifyType = NotifyType.ON_FOCUS_CHANGE,
) : ValidatableRule(
errorMessage = errorMessage,
rule = { input -> input.isNotEmpty() }
Expand Down
Loading

0 comments on commit 1eb3a07

Please sign in to comment.