ba4f27f9ba
There's a new `validators` property that can be used to add validators to `StatusInput` instances, which are executed in the same order: ```qml StatusInput { text: "Some value" validators: [...] } ``` For convenience StatusQ provides some common validation methods, such as `StatusMinLengthValidator`, StatusMaxLengthValidator` and could be extended to other (e.g. email validation etc): ```qml StatusInput { text: "Some value" validators: [ StatusMinLengthValidator { minLength: 3 } ] } ``` Validators are executed every time the text of the input changes. They are executed in the same order they have been applied, which enables users to create cascading conditions like "First make sure the value is at least 3 characters long, then make sure it matches a certain pattern". When a validation fails, it sets the validity of the input (`valid: false`) accordingly and optionally exposes additional error information on `StatusInput.errors`: ```qml StatusInput { text: "Fo" onTextChanged: { if (errors) { /** * errors now has the following structure: * errors: { * minLength: { minValue: 3, actual: ... } * } * Also, `StatusInput` is now `valid = false` **/ errorMesssage = "Expected " + errors.minLenght.minValue + " but got: "+ errors.minLength.actual; // i18n'able } } validators: [ StatusMinLengthValidator { minLength: 3 } ] } ``` There can be any number of error objects on the `errors` property, depending on who many validators have been run that failed validation. Custom validators can be implemented by introducing a new `StatusValidator` type that has to implement a `validate()` function and defines the validators name. The `validate()` function has to return either `true` or `false` depending on whether the value is valid. Alternatively, the function can return an error object which gets exposed on the underlying input's `errors` property, at which point it's considered invalid as well. Here's a simple custom validator: ```qml // HelloValidator.qml import StatusQ.Controls.Validators 0.1 StatusValidator { property string name: "hello" validate: function (value) { // `value` is the `text` value of the underlying control return value === "hello" } } ``` Applying this validators would look like this: ```qml StatusInput { text: "Some value" validators: [ HelloValidator {} ] onTextChanged: { if (errors.hello) { errorMessage = "Doesn't say hello!" } } } ``` Alternatively, validators can return error objects to provide more information about what went wrong. Here's the implementation of the `StatusMinLengthValidator` as an example: ```qml StatusValidator { property int minLength: 0 name: "minLength" validate: function (value) { return value.length >= minLength ? true : { min: minLength, actual: value.length } } } ``` Because validators as components, they can hold any custom properties they need to be configured. There has been concern that, with this API, error messages need to be potentially defined in multiple places, given that there could be multiple instances of any validator. This is easily solved by having a centralized function figure out what the error message is, given a certain error object: ```qml StatusInput { validators: [ StatusMinLengthValidator { minLength: 3 } ] onTextChanged: { if (errors) { errorMessage = getErrorMessage(errors) // this function is provided by global or elsewhere } } } ``` Closes #298 |
||
---|---|---|
sandbox | ||
src | ||
.gitignore | ||
CHANGELOG.md | ||
README.md | ||
statusq.qrc |
README.md
StatusQ
An emerging reusable QML UI component library for Status applications.
Usage
StatusQ introduces a module namespace that semantically groups components so they can be easily imported. These modules are:
- StatusQ.Core
- StatusQ.Core.Theme
- StatusQ.Components
- StatusQ.Controls
- StatusQ.Layout
- StatusQ.Platform
- StatusQ.Popups
Provided components can be viewed and tested in the sandbox application that comes with this repository. Other than that, modules and components can be used as expected.
Example:
import Status.Core 0.1
import Status.Controls 0.1
StatusInput {
...
}
Viewing and testing components
To make viewing and testing components easy, we've added a sandbox application to this repository in which StatusQ components are being build. This is the first place where components see the light of the world and can be run in a proper application environment.
Using Qt Creator
The easiest way to run the sandbox application is to simply open the provided sandbox.pro
file using Qt Creator.
Using command line interface
To run the sandbox from within a command line interface, run the following commands:
$ git clone https://github.com/status-im/StatusQ
$ cd StatusQ/sandbox
$ ./scripts/build
Once that is done, the sandbox can be started with the generated executable:
$ ./bin/sandbox
More Documentation available on the wiki