Form.process
$form.process($processFunction) -> null
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.
NoteYou will usually want to make this call inside of a
mainPost
function. This function is automatically called by THT when the page is requested as a POST
method (i.e. a form submission).Process Function
If the data passes validation, $processFunction
will be called, with the first argument being a map containing the now-sanitized input data.
fun mainPost { $form.process(fun ($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(fun ($data) { if !$data.password.check($correctPasswordHash) { return ['password', 'Incorrect password.'] } ... })
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 -- fun mainPost { @@.emailForm.process(fun ($data) { $row = { email: $data.email } Db.insertRow('emails', $row) return url'/home' }) }