Version: v0.7.0 - Beta.  We welcome contributors & feedback.

Form.process

$form.process($processFunction) -> false

Description

Validate the user-submitted data and then pass it to $processFunction.

If the data does not pass validation, it will automatically return an error message to the browser.

Process Function

If the data passes validation, $processFunction will be called, with the first argument being a map containing the now-sanitized input data.

fn mainPost {

    $form.process(fn ($data) {

        // Update the database, etc...
    })
}

When the function exits, a response will be sent to the browser with an ok status.

Custom Validation

You can trigger custom validation errors by returning a List in the form of [$fieldName, $errorMessage].

Example:

$form.process(fn ($data) {

    if $data.userName.contains('evil') {
        return ['userName', 'Evil users are not allowed.']
    }

    ...
})

Redirects

If the function returns a URL TypeString (url'...'), the browser will redirect to the given URL.

If the function returns true, the browser will reload the current URL.

Example:

@@.emailForm = Form.create('emailForm', {
    email: {
        rule: 'email',
    },
})

// -- snip form template --

fn mainPost {

    @@.emailForm.process(fn ($data) {

        $row = { email: $data.email }
        Db.insertRow('emails', $row)

        return url'/home'
    })
}

See Also