Beagle v1.5 is no longer actively maintained. The documented version you are viewing may contain deprecated functionality. For up-to-date documentation, see the latest version .
A validator component is used when it is necessary to validate some form information. It validates through isValid
method that is requested when a FormSubmit
or a notifyChanges()
are implemented. This validator is listed as component that receives data in a form named FormInput.
The Form component capture data informed by the user and validates and send them to BFF. This validation can happen when the user activates FormSubmit or when notifies its status to InputWidget(FormInput).
Validator receives two generic parameters and it has only on isValid method that returns a boolean value.
interface Validator<in I, in W> {
fun isValid(input: I, widget: W): Boolean
}
inputWidget
sends as data to be validated.InputWidget
that sent the data.To implement a validator, you have to have a form that enables FormSubmit when FormInput is not empty, in this case it will receive a string with the data and then it will be validated if it is empty or not.
@RegisterValidator
@RegisterValidator("text-is-not-blank")
class TextNotBlankValidator : Validator<String, ServerDrivenComponent> {
override fun isValid(input: String, widget: ServerDrivenComponent): Boolean {
return !input.isBlank()
}
}
InputWidget
this validation, it is necessary to refer the given name to the annotation above "text-is-not-blank"
in the component. Follow the example below:FormInput(
name = "required-field",
required = true,
validator = "text-is-not-blank",
child = SampleTextField(placeholder = "placeholder")
)
The follow example, the InputWidget
implementation will run the Validator calling the notifyChanges()
method.
@RegisterWidget
class SampleTextField(private val placeholder: String) : InputWidget() {
@Transient
private lateinit var textFieldView: EditText
override fun getValue() = textFieldView.text.toString()
override fun onErrorMessage(message: String) {
textFieldView.error = message
}
override fun toView(context: Context) = EditText(context).apply {
textFieldView = this
textFieldView.hint = placeholder
textFieldView.isSingleLine = true
doOnTextChanged { _, _, _, _ -> notifyChanges() }
}
}
notifyChanges()
inside the other function doOnTextChange{}.
Was this page helpful?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please tell us how we can improve.