Skip to content

Custom fields ​

When none of the built-in inputs fits (a color swatch, a map, a code editor), an application can ship its own form input: a widget written in plain JavaScript, loaded by the panel from an extension script embedded in the application binary. The server still validates the value by the field's kind and rules; the widget only edits it. For data without a column (stored by the application in its own tables), see virtual fields.

The category form with the color swatch widgetA RustCMS demo application: the demo/color widget on the category form.

Quick start ​

sh
rustcms make:widget acme/color

This writes resources/admin/js/acme/color.js (a starting point that registers acme/color) and prints the two lines to add:

rust
// src/providers/admin_panel_provider.rs
.extension_script("acme-color-…", include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/resources/admin/js/acme/color.js")))

// The resource's form: the input names its widget.
CustomInput::make("color", "acme/color")

The extension name it prints is the widget name with / replaced by -, followed by a short hash, so two widget names never share one. The command never overwrites a file.

Full example ​

The demo's category color is an ordinary text column with a length rule:

rs
Field::column(category::Column::Color).max_length(7),

Its form edits it with the demo/color widget and passes the palette as props (JSON, at most 4 KiB, checked at boot):

rs
// Edited by the demo's extension widget (resources/admin/js/demo/color.js).
CustomInput::make("color", "demo/color").props(json!({"palette": [
    {"value": "#dc2626", "label": "Red"},
    {"value": "#16a34a", "label": "Green"},
    {"value": "#2563eb", "label": "Blue"},
    {"value": "#9333ea", "label": "Purple"},
]})),

The provider loads the script and its stylesheet:

rs
// The `demo/color` widget of the category form.
.extension_script("demo-color", include_str!("../../resources/admin/js/demo/color.js"))
.extension_style("demo-color", include_str!("../../resources/admin/css/demo/color.css"))

The widget itself:

js
window.rustcms.registerTranslations("vi", {
  None: "Không màu",
  Red: "Đỏ",
  Green: "Xanh lá",
  Blue: "Xanh dương",
  Purple: "Tím",
});
window.rustcms.registerWidget("demo/color", {
  apiVersion: 1,
  mount(element, ctx) {
    let current = ctx;
    const group = document.createElement("div");
    group.className = "demo-color";
    group.setAttribute("role", "group");
    const buttons = (ctx.props?.palette ?? []).map(({ value, label }) => {
      const button = document.createElement("button");
      button.type = "button";
      button.className = "demo-color-swatch";
      button.dataset.color = value;
      button.dataset.label = label;
      button.addEventListener("click", () => current.onChange(value));
      return button;
    });
    const clear = document.createElement("button");
    clear.type = "button";
    clear.className = "demo-color-clear";
    clear.addEventListener("click", () => current.onChange(null));
    group.append(...buttons, clear);
    element.append(group);
    const render = () => {
      group.id = current.id;
      group.setAttribute("aria-label", current.label);
      // Labels in the panel's language (`update` is called when it changes).
      for (const button of buttons) {
        const label = current.t(button.dataset.label);
        button.setAttribute("aria-label", label);
        button.title = label;
      }
      clear.textContent = current.t("None");
      if (current.describedBy) group.setAttribute("aria-describedby", current.describedBy);
      else group.removeAttribute("aria-describedby");
      for (const button of [...buttons, clear]) button.disabled = current.disabled;
      for (const button of buttons) button.setAttribute("aria-pressed", String(button.dataset.color === current.value));
      clear.setAttribute("aria-pressed", String(current.value == null));
      group.classList.toggle("demo-color-invalid", current.invalid);
    };
    render();
    return {
      update(next) {
        current = next;
        render();
      },
      unmount() {
        group.remove();
      },
    };
  },
});

Names and assets ​

  • Widget names are vendor/name, each part 1–32 of a-z, 0-9, -.
  • Extension names (the first argument of extension_script and extension_style) are 1–64 of the same characters. A name may serve once as a script and once as a stylesheet; adding the same script or stylesheet name twice stops the boot.
  • Each extension is served from {admin}/assets/ext/<name>.<hash>.js|css, with a hash of its content, and cached as immutable.
  • Stylesheets are inserted in the order they are registered (their loading is not awaited). Scripts load one after the other, in registration order, and the panel renders once they have loaded or timed out.
AdminProvider methodEffect
extension_script(name, js)a v1 script (widgets, script pages, slots, cells)
extension_style(name, css)a stylesheet (inline styles are not allowed)
extension_script_v2(name, js, Requires { … })a React script: pages, slots and cells only
extension_timeout(Duration)how long the panel waits for each script (1–60 s, default 10; panics outside that range)

The widget contract (API version 1) ​

js
window.rustcms.registerWidget("acme/color", {
  apiVersion: 1,
  mount(element, ctx) {
    // Build the UI inside `element`; report changes with ctx.onChange(value).
    return {
      update(ctx) { /* the value, disabled or invalid state changed */ },
      unmount() { /* remove listeners; the panel empties `element` */ },
    };
  },
});
ctx
idput it on the main control: it is the label's target
labelthe field's label, as the form shows it
valuethe field's wire value, null, or undefined when not set
disabled, invalidthe field's state
describedByids of the help and error texts, for aria-describedby
propsthe JSON given to CustomInput::props
locale, t(text)the panel's language and your catalogs
onChange(value)the only way to change the form
  • value is a string for decimals and 64-bit integers, an ISO string for dates. It is a copy: changing it does nothing.
  • onChange(value): undefined means "not set" (the field is left out of the request); null sends null. Calls while disabled, or after unmount, are ignored. mount and update must not call it.
  • update is called only when something changed, including the language. React's development mode may mount, unmount and mount again: unmount must undo mount.
  • Widgets translate their own text with ctx.t, from catalogs registered while the script loads (window.rustcms.registerTranslations("vi", { None: "Không màu" }); Languages). Props are never translated.
  • Register widgets while the script runs. The registry is frozen once all extensions are loaded, and a script that missed its deadline can no longer register anything. A name can be registered once.

Widgets are v1 only

Field widgets exist only in this DOM API. The React SDK (React pages) has pages, slots and cells, but no React field widget.

Failures ​

A field whose widget failed: the stored value shown read-onlyA RustCMS demo application: the fallback of a widget that failed.

  • A script that fails to load, or takes longer than AdminProvider::extension_timeout, is skipped (the error is in the browser console).
  • A widget that throws, returns no {update, unmount}, or reports a number JSON cannot carry exactly (NaN, Infinity, an integer outside −(2^53−1) to 2^53−1) is disabled for the rest of the page's life, in every field that uses it.

In both cases its fields are shown read-only (the stored JSON) and never sent; a required one blocks the form with "cannot be edited here: its widget is not available".

Trust and CSP ​

Extensions are application code, not user content. They run with the panel's full privileges (the user's session, every API call), also before sign-in, and the panel's CSP does not isolate them from each other. Only ship code you review.

The CSP still applies to them, and it is not something to loosen: no inline scripts, no style attributes or <style> elements (use extension_style), images only from the panel or data: URLs. Build elements with the DOM rather than writing innerHTML with data.

The server never trusts the widget: the value it sends is checked against the field's kind and rules, scope() and the policy, like any other input.

Pages, slots and cells ​

The same extension scripts may draw custom pages (registerPage), content beside records (registerSlot), table cells (registerCell) and the entries of a record's view page (registerEntry, see Infolists), with the same lifecycle (mount, update, unmount): see Customizing the panel and custom cells (TableColumn::cell("vendor/name") with props, also at most 4 KiB). React versions of pages, slots and cells are React pages.

RustCMS 0.17.0 · source cfb82fa33fab · MIT OR Apache-2.0