You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Yves Kalume edited this page Dec 1, 2023
·
2 revisions
Core Concepts
validable
A validable represents your field, it contains the text, error message and rules of the field.
By default we have two validables
Required field
val nameField = remember { NotEmptyValidable() }
Email
val emailField = remember { EmailValidable() }
Validable properties
myfield.value // returns the text
myfield.errorMessage // returns the error (null if the field is valid)
myfield.hasError() // return false if the field is valid, return true if it is not valid
Submitting text fields
text fields can be submitted with a withValidable block such as :
// pass the field to the withValidable block
withValidable(emailField) {
// will be executed if all fields are valid
}
if you have multiple text fields it will look like this:
// pass all fields to the withValidable block
withValidable(field1,field2,field3) {
}
Create custom rules
you can create your custom validables, for that your class will have to extend BaseValidable which takes two functions as parameters
classMyCustomValidable : BaseValidable(validator = ::isFieldValid, errorFor = ::fieldValidationError)
privatefunisFieldValid(text:String): Boolean {
return text.isNotBlank() // the condition that must be met
}
privatefunfieldValidationError(text:String) : String {
return"This field is required"// the error message that will be displayed
}
this last one will be used like the other validables