Skip to main content

Widget

Struct Widget 

Source
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 form name=. Also the key the POST handler form_map.get(...) reads back on submit.
  • kind — picks the input type. See WidgetKind.
  • custom_name — wire name for a WidgetKind::Custom registration. Looked up at render time via registry::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 HTML5 required attribute.
  • min / max / step — clamps for numeric / date / range inputs.
  • max_length — max-length cap for string inputs.
  • read_only — emits HTML5 readonly; for fully-non-editable display use the page-level crate::page_type::DisplayField instead.

Fields§

§name: String§kind: WidgetKind§custom_name: String§custom_html: String

Pre-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: String

String 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: String

HTML5 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: String

Style 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

Source

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.

Source

pub fn with_min_length(self, n: u32) -> Self

Fluent: set HTML5 minlength.

Source

pub fn with_pattern(self, p: impl Into<String>) -> Self

Fluent: set HTML5 pattern regex.

Source

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).

Source

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).

Source

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.

Source

pub fn with_value(self, v: impl Into<String>) -> Self

Source

pub fn with_help(self, h: impl Into<String>) -> Self

Source

pub fn with_placeholder(self, p: impl Into<String>) -> Self

Source

pub fn required(self) -> Self

Source

pub fn read_only(self) -> Self

Source

pub fn with_options<I, V, L>(self, options: I) -> Self
where I: IntoIterator<Item = (V, L)>, V: Into<String>, L: Into<String>,

Source

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())))]
Source

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)]
Source

pub fn with_min(self, m: f64) -> Self

Source

pub fn with_max(self, m: f64) -> Self

Source

pub fn with_step(self, s: impl Into<String>) -> Self

Source

pub fn with_max_length(self, n: u32) -> Self

Trait Implementations§

Source§

impl Clone for Widget

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Widget

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for Widget

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Serialize for Widget

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<Unshared, Shared> IntoShared<Shared> for Unshared
where Shared: FromUnshared<Unshared>,

Source§

fn into_shared(self) -> Shared

Creates a shared type from an unshared type.
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more