« All Handlers

onFormsSubmission

LiveWhale will automatically validate form fields against expected values, e.g. that a successful form submission requires that an email field receive either a single email address or a series of comma-separated email addresses. LiveWhale also automatically enforces that required field are not empty (‘’).

If you are adding custom fields to an existing data module, or simply want to enforce additional checks on existing fields, then you can accomplish this with a custom module that utilizes an onFormsSubmission handler. An example module is below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?php
$_LW->REGISTERED_APPS['my_app']=[
'title'=>'My App',
'handlers'=>['onFormsSubmission'],
];

class LiveWhaleApplicationMyApp {

// perform additional form validations
public function onFormsSubmission($id) {
global $_LW;
// use the $id to only apply this validation to a certain form, or perhaps
// track the presence of a field name in $_LW->_POST
if ($id === 6) {
// sanitized field values are available in $_LW->_POST, which is recommended
// all the field name are abstracted, but consistent, so you’ll have to pick
// them out. for example, require that the title is not too long
if (strlen($_LW->_POST['lw_form_6_###############']) > 60) {
die(json_encode(['error' => 'Brevity is the soul of wit. (Shakespeare)
Please shorten your title to less than 60 characters.']));
}
// and don’t forget about summary length
if (strlen($_LW->_POST['lw_form_6_###############']) > 300) {
die(json_encode(['error' => 'Brevity makes sweetness, doesn\'t it?
(Stephen King) Please shorten your summary to less than 300 characters.']));
}
// validations succeeded
}
// no validations needed
}
}
?>