1 min read
InputFormFields
InputFormFields are a set of views made to create form screens easier. All these views have in common the interface InputFormField<T>
where T
is the class type to be validated. The available InputFormFields are:
- InputFormTextInputLayout
- InputFormAutocompleteTextInputLayout
- DateOfBirthTextInputLayout
- EmailTextInputLayout
- GenderPickerTextInputLayout
- PasswordTextInputLayout
- RetypePasswordInputFormView
It also provides an extension function to find and validate all InputFormFields found as descendants of a ViewGroup, not necessarily direct descendants. This method simplifies adding new InputFormFields to existing PoqSDK form screens, you can add new fields to the layout without needing to call validate()
for each new view.
fun ViewGroup.validateAllInputFormField(): PoqResult<Unit, List<InputFormField<*>>>
InputFormFields are included in the account.inputform
module which is included by platform module. If you need to include this module, add it to your build.gradle
:
implementation ("com.poqstudio:account.inputform:$VERSION_NAME")
Validations
InputFormFields work through Validation
objects which can be added to the InputValidator
of each InputFormField via the addValidation(validation: Validation<T>)
method. The Validation
class is compose of:
ErrorMessage
: it can be a text or an id of a String resource for the text to be displayed when the validations fails.- Validation function
(T) -> Boolean
:T
is the value to be validated. Returnsfalse
if it is not valid,true
otherwise.
These are the available Validations in the PoqSDK:
You can create your own Validation creating a class that extends the Validation
class.
How to create a custom InputFormField
If you need to implement a new InputFormField, you can:
Add more Validations to an existing InputFormField.
inputFormField.inputValidator.addValidation(YourNewValidation())Extend one of the available InputFormFields.
Create a new one from scratch. To create a new one you need to:
- Create a custom view that implements
InputFormField<T>
whereT
is the class type to be validated. - Provide an implementation in your custom view for
InputValidator<T>
, you can use the generic PoqSDK implementationPoqInputValidator<T>
- Add as many validations as you need in your view calling
inputFormField.addValidation(validation: Validation<T>)
- Implement
validate()
in your custom view callinginputValidator.validate(T)
- Create a custom view that implements
As an example, if you need to create an inputFormField for a SwitchMaterial
, you can do:
class CustomInputFormField : SwitchMaterial, InputFormField<Boolean> {
constructor(context: Context) : super(context) constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)
override val inputValidator: InputValidator<Boolean> = PoqInputValidator() //You can inject this with Koin
init { inputValidator.addValidation(CustomValidation()) }
override fun validate(): Boolean = inputValidator.validate(isChecked)}