papyros / qml-material

:book: Material Design implemented in QtQuick
GNU Lesser General Public License v2.1
2.56k stars 476 forks source link

form validation with rules #235

Open FiloSpaTeam opened 9 years ago

FiloSpaTeam commented 9 years ago

if can be usefull i've implemented a stateless library that can be used to validate real time, manually, every x time, an object and use appropriate error of that.

Example of code:

        TextField {
            id: inputName
            placeholderText: qsTr("Name")
            floatingLabel: true
            onTextChanged: ErrorValidator.checkTextField(inputName, ["required", "regexp:^[A-Za-z]+$"])
        }

        TextField {
            id: inputEmail
            placeholderText: qsTr("Email")
            helperText: "john@example.com"
            floatingLabel: true

            inputMethodHints: Qt.ImhEmailCharactersOnly | Qt.ImhNoPredictiveText | Qt.ImhNoAutoUppercase

            onTextChanged: ErrorValidator.checkTextField(inputEmail, ["required", "email"])
        }

NoError: noerror

Error1: error1

Error2: error2

Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

FiloSpaTeam commented 9 years ago

Ok, i've done a good modification over this library.

Example of JSON rules:

function rules() {
    return {
        "login" : {
            "nome" : {
                "input" : inputEmail,
                "type"  : "TextField",
                "rules" : ["required", "email"]
            },
            "password" : {
                "input" : inputPassword,
                "type"  : "TextField",
                "rules" : ["required", "min:8", "max:16"]
            }
        },
        "registrati" : {
            "nome" : {
                "input" : inputNome,
                "type"  : "TextField",
                "rules" : ["required", "regexp:^[A-Za-z]+$", "min:2"]
            },
            "email" : {
                "input" : inputEmail,
                "type"  : "TextField",
                "rules" : ["required", "email"]
            },
            "password" : {
                "input" : inputPassword,
                "type"  : "TextField",
                "rules" : ["required", "min:8", "max:16"]
            },
            "password_confirmation" : {
                "input" : inputConfermaPassword,
                "type"  : "TextField",
                "rules" : ["required", "min:8", "max:16", "confirmation:" + inputPassword.text]
            }
        }
    }
}

Example of signal use (TextField):

onTextChanged: ErrorValidator.checkTextField(rules[base.state]["email"])

Example of global check (example on confirm click):

if (ErrorValidator.check(rules, base.state))
// VALID CODE
maxvanceffer commented 9 years ago

Can you provide source of rule.js please