Events: on_post() ... on_after_update() form()
Callbacks at each step of a form: POST, render, validation, the DB write and the success notice.
form()->on_post($func)
form()->on_before_render($func)
form()->on_after_render($func)
form()->on_before_validate($func)
form()->on_after_validate($func)
form()->on_validate_ok($func)
form()->on_validate_error($func)
form()->on_success_text($func)
form()->on_before_update($func)
form()->on_after_update($func)Options
| Option | Meaning |
|---|---|
on_post | fn ($extra, $replace, $form): every POST of the page, before validation (not on a failed token with SKIP_ON_POST_ON_CSRF_FAIL). |
on_before_render, on_after_render | fn ($extra, $replace, $form) before the rows are drawn and after. |
on_before_validate | fn ($rules, $data, $form). |
on_validate_ok, on_validate_error | fn ($data, $extra, $rules, $form). |
on_after_validate | fn ($ok, $rules, $data, $extra, $form). |
on_before_update, on_after_update | fn ($data, $table, $fields, $type, $extra, $form) around the write of db_insert_if_ok(). |
on_success_text | A text (not a callback): the notice after the write. |
Example
Called on this request: on_before_render, on_after_render
Code
$log = [];
$note = function ($text) use (&$log) {
return function () use (&$log, $text) {
$log[] = $text;
};
};
$html = (string) form(['title' => 'Spring sale'])
->on_before_render($note('on_before_render'))
->on_after_render($note('on_after_render'))
->on_post($note('on_post'))
->on_before_validate($note('on_before_validate'))
->on_validate_ok($note('on_validate_ok'))
->on_validate_error($note('on_validate_error'))
->on_after_validate($note('on_after_validate'))
->on_before_update($note('on_before_update'))
->on_after_update($note('on_after_update'))
->on_success_text('Saved.')
->validate(['title' => 'trim|required'])
->db_update_if_ok('static_pages', ['title'], 7, ['no_redirect' => 1])
->text('title')
->save();
return $html . '<pre>' . _prepare_html('Called on this request: ' . implode(', ', $log)) . '</pre>';Notes
- Post the form to see the POST, validation and update events; nothing is written on this site.
- Each method keeps one callback; the same keys in the
validate()/db_*_if_ok()options win. Global eventsform.before_render,form.validate_ok,form.after_update... fire oncore_events.
See also: form/validate, form/db_insert_if_ok, table/on_post, demo:form/events