Forms
Introduction
Forms are one of the most important parts of GUI of many applications, especially the ones which collect and manage data from the users. Traditionally handling HTML forms is a complex process, composed of many small steps like placing HTML form controls on the page, setting and displaying initial data, reading user data from individual HTML elements and finally transforming and validating the data. It gets even worst if you are using custom visual components for complex data types - date, time, rich text, multiple select, uploaded files etc.
KVision lets you work with forms in a very simple, consistent and efficient way. It hides all complex data transformations inside the framework logic and offers you a fully type-safe binding between your data and your forms. It has support for many different, both simple and complex, form controls. And it gives you ready to use data validation.
Form data model
Form data is modeled with a standard Kotlin data class enhanced with @Serializable
annotation from kotlinx.serialization library. Every field of this model class holds the value of one input item rendered within a form. The model support most basic data types: String
, Number
(including Int
and Double
), Boolean
, Date
and also a special type List<KFile>
(a list of uploaded files). All properties of the model data class should have default values.
Note: You have to add @ContextualSerialization
annotations to your Date
fields in order to explicitly allow serialization with the KVision context. You can also use @file:ContextualSerialization(Date::class)
file annotation if you want to keep your model classes cleaner. You can find more information about this annotation in the kotlinx.serialization documentation.
Form controls
Form controls are KVision components implementing one of five FormControl
interfaces inside pl.treksoft.kvision.form
package: StringFormControl
, NumberFormControl
, BoolFormControl
, DateFormControl
and KFilesFormControl
. KVision comes with a bunch of build-in or modular form components.
Component | Interface | Module | Description |
|
| built-in | A check-box. |
|
| built-in | A radio-button. |
|
| built-in | A group of radio-buttons. |
|
| built-in | A text field. |
|
| built-in | A text field for password input. |
|
| built-in | A text area. |
|
| kvision-datetime | A date and/or time selection control. |
|
| kvision-richtext | A rich text editor. |
|
| kvision-select | A select box with support for multiple selection and AJAX data source support. |
|
| kvision-select-remote | A select box for multi-platform server-side connectivity. |
|
| kvision-spinner | A spinner control for number selection. |
|
| kvision-upload | An upload file control with preview and multi-selection. |
Note: RadioGroup
and Select
controls always return String
values. Multiple selections are comma-separated.
Form containers
KVision provides a dedicated container for working with forms - FormPanel<K>
. To create a FormPanel<K>
instance you need to specify a serializer for your model data class.
You can also use a DSL builder extension function, which automatically uses default serializer for your data class.
Under the hood FormPanel
container uses non-visual form container - Form<K>
, which can be used by developers to implement their own form containers.
Data binding
You add form controls to the FormPanel
using add
method of the container, and at the same time you bind your controls to your data model by referencing class properties. The binding is type-safe, e.g. you can't bind StringFormControl
to Boolean
or Date
field.
After binding your fields you can treat a FormPanel<K>
instance as a kind of a "black box" - you manage all your data flow with just a few methods - mostly setData()
and getData()
.
Form parameters
FormPanel<K>
container can layout form elements with three standard Bootstrap layouts: normal, horizontal and inline. The class constructor allows to specify other HTML form parameters as well.
Validation
KVision forms support validation for single fields and for the form as a whole. You can easily mark some fields as required, specify all needed validation functions and specify error messages, which will be displayed by the browser after validation action. Validation functions give you easy access to the values entered in the form.
Note: validatorMessage
parameters are functions with the same parameters as validator
functions.
Note: requiredMessage
and validatorMessage
parameters are optional. The default values for them are respectively "Value is required" and "Invalid value". You should use these parameters if you want to give more precise descriptions of the problems or when you want to internationalize your application.
Last updated