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

OptionMeaning
on_postfn ($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_renderfn ($extra, $replace, $form) before the rows are drawn and after.
on_before_validatefn ($rules, $data, $form).
on_validate_ok, on_validate_errorfn ($data, $extra, $rules, $form).
on_after_validatefn ($ok, $rules, $data, $extra, $form).
on_before_update, on_after_updatefn ($data, $table, $fields, $type, $extra, $form) around the write of db_insert_if_ok().
on_success_textA 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 events form.before_render, form.validate_ok, form.after_update... fire on core_events.

See also: form/validate, form/db_insert_if_ok, table/on_post, demo:form/events