pub struct Widget {Show 19 fields
pub name: String,
pub kind: WidgetKind,
pub custom_name: String,
pub custom_html: String,
pub label: String,
pub value: String,
pub options: Vec<(String, String)>,
pub help: String,
pub placeholder: String,
pub required: bool,
pub min: Option<f64>,
pub max: Option<f64>,
pub step: Option<String>,
pub max_length: Option<u32>,
pub min_length: Option<u32>,
pub pattern: String,
pub read_only: bool,
pub allowed: Vec<String>,
pub variant: String,
}Expand description
Runtime descriptor for one rendered form input. Built by
PageTypeHandler::extension_fields (and, in a later slice, by
Block::fields → BlockField::Widget) and consumed by the
_widget.html Tera macro.
§Fields
name— HTML formname=. Also the key the POST handlerform_map.get(...)reads back on submit.kind— picks the input type. SeeWidgetKind.custom_name— wire name for aWidgetKind::Customregistration. Looked up at render time viaregistry::find_custom_widget. Ignored for built-in kinds.label— display label above / next to the input.value— pre-fill value. For multi-value widgets (WidgetKind::Checkboxes/WidgetKind::MultiSelect) the value is the JSON-array string the client POSTs back verbatim.options—(value, label)pairs for choice widgets.help— short hint rendered under the input.placeholder—placeholder=attribute for text-shaped inputs.required— emits the HTML5requiredattribute.min/max/step— clamps for numeric / date / range inputs.max_length— max-length cap for string inputs.read_only— emits HTML5readonly; for fully-non-editable display use the page-levelcrate::page_type::DisplayFieldinstead.
Fields§
§name: String§kind: WidgetKind§custom_name: String§custom_html: StringPre-rendered HTML for WidgetKind::Custom widgets — the
admin handler walks each widget, looks up the registered
template via registry::find_custom_widget, renders it
once before stamping into Tera context, and stores the
result here. The bundled _widget.html macro emits it
| safe. Empty for non-Custom widgets.
label: String§value: String§options: Vec<(String, String)>Choice options as (value, label) pairs. Always serialized
(even when empty) so the bundled _widget.html macro’s
{% for opt in w.options %} loops don’t throw an
undefined-variable error on widgets that don’t use options.
help: StringString fields below are always serialized (empty = falsy in
Tera) so {% if w.foo %} checks in _widget.html don’t
trip Tera’s “Variable not found” error when the slot is
unset. Same rule applies to variant, pattern, etc.
placeholder: String§required: bool§min: Option<f64>§max: Option<f64>§step: Option<String>§max_length: Option<u32>§min_length: Option<u32>HTML5 minlength for string-shaped widgets.
pattern: StringHTML5 pattern regex for string-shaped widgets — browser
validates on form submit.
read_only: bool§allowed: Vec<String>For WidgetKind::Stream only — the block type_names
permitted at the top level of this stream. Boot-time
validation in crate::block::validate_block_registry
verifies every entry resolves to a registered
crate::Block. Empty for non-Stream widgets.
variant: StringStyle variant — handler opt-in to a visual variant of the
canonical widget. Today only "switch" is recognised by the
_widget.html macro (on WidgetKind::Boolean, renders the
checkbox as an iOS-style toggle). Empty = use the default.
Implementations§
Source§impl Widget
impl Widget
Sourcepub fn new(
kind: WidgetKind,
name: impl Into<String>,
label: impl Into<String>,
) -> Self
pub fn new( kind: WidgetKind, name: impl Into<String>, label: impl Into<String>, ) -> Self
Smallest possible widget — just (kind, name, label). Caller
fluently adds the rest via .with_* methods.
Sourcepub fn with_min_length(self, n: u32) -> Self
pub fn with_min_length(self, n: u32) -> Self
Fluent: set HTML5 minlength.
Sourcepub fn with_pattern(self, p: impl Into<String>) -> Self
pub fn with_pattern(self, p: impl Into<String>) -> Self
Fluent: set HTML5 pattern regex.
Sourcepub fn with_variant(self, variant: impl Into<String>) -> Self
pub fn with_variant(self, variant: impl Into<String>) -> Self
Style variant opt-in. Currently meaningful for
WidgetKind::Boolean ("switch" flips to iOS-style toggle).
Sourcepub fn stream<S: Into<String>>(
name: impl Into<String>,
label: impl Into<String>,
allowed: impl IntoIterator<Item = S>,
) -> Self
pub fn stream<S: Into<String>>( name: impl Into<String>, label: impl Into<String>, allowed: impl IntoIterator<Item = S>, ) -> Self
Shortcut for a WidgetKind::Stream widget — pairs the
name / label with the list of allowed top-level block
type_names. Every entry must resolve to a registered
crate::Block (boot-time validation enforces this).
Sourcepub fn custom(
custom_name: impl Into<String>,
name: impl Into<String>,
label: impl Into<String>,
) -> Self
pub fn custom( custom_name: impl Into<String>, name: impl Into<String>, label: impl Into<String>, ) -> Self
Shortcut for a WidgetKind::Custom widget — pairs the
kind with the third-party registration custom_name looked
up at render time through registry::find_custom_widget.
pub fn with_value(self, v: impl Into<String>) -> Self
pub fn with_help(self, h: impl Into<String>) -> Self
pub fn with_placeholder(self, p: impl Into<String>) -> Self
pub fn required(self) -> Self
pub fn read_only(self) -> Self
pub fn with_options<I, V, L>(self, options: I) -> Self
Sourcepub fn snippet_m2m(
relation_name: impl Into<String>,
label: impl Into<String>,
snippet_type_name: impl Into<String>,
) -> Self
pub fn snippet_m2m( relation_name: impl Into<String>, label: impl Into<String>, snippet_type_name: impl Into<String>, ) -> Self
Convenience constructor for the Page↔Snippet M2M
chooser. The widget’s name becomes the M2M
relation_name (the field on the page that this chooser
populates), and snippet_type_name lands in
Self::custom_name where the template reads it. Hosts
then call Self::with_options with the eligible
(snippet_id, snippet_title) pairs they fetched from
cms_snippet for that type — until the derive-macro hook
lands and does the population automatically.
use rustango_cms::widget::Widget;
// In the handler's `widgets()` impl, after fetching
// `let cats = Snippet::objects().where_(...).fetch(pool).await?;`
vec![Widget::snippet_m2m("categories", "Categories", "Category")
.with_options(cats.iter().map(|s|
(s.id.get().copied().unwrap_or_default().to_string(),
s.title.clone())))]Sourcepub fn model_chooser(
field_name: impl Into<String>,
label: impl Into<String>,
chooser_slug: impl Into<String>,
) -> Self
pub fn model_chooser( field_name: impl Into<String>, label: impl Into<String>, chooser_slug: impl Into<String>, ) -> Self
Build a generic model-chooser widget for the
register_chooser!ed model registered under chooser_slug.
The value is the chosen row’s id (stringified). The slug rides
in Self::custom_name → data-chooser-kind → the cms-ux.js
overlay’s /cms-admin/__chooser/<slug> endpoint.
use rustango_cms::widget::Widget;
// In a PageTypeHandler's `widgets()` impl:
vec![Widget::model_chooser("author_id", "Author", "author")
.with_value(stored_id)]pub fn with_min(self, m: f64) -> Self
pub fn with_max(self, m: f64) -> Self
pub fn with_step(self, s: impl Into<String>) -> Self
pub fn with_max_length(self, n: u32) -> Self
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Widget
impl<'de> Deserialize<'de> for Widget
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Auto Trait Implementations§
impl Freeze for Widget
impl RefUnwindSafe for Widget
impl Send for Widget
impl Sync for Widget
impl Unpin for Widget
impl UnsafeUnpin for Widget
impl UnwindSafe for Widget
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more