Skip to main content

ChangesetForm

Struct ChangesetForm 

Source
pub struct ChangesetForm<T> {
    pub changeset: Changeset<T>,
    /* private fields */
}
Expand description

Axum extractor that decodes a form body, runs validation, and captures the CSRF token — all in one step.

Supports both application/x-www-form-urlencoded (always) and multipart/form-data (when the multipart feature is enabled).

Unlike crate::validation::Valid, this extractor never rejects with 422 — errors live in the Changeset and the handler decides how to respond. Fails with 400 only when the body cannot be decoded into T at all.

§CSRF — no extra developer action in POST handlers

The extractor reads the CsrfToken from request extensions (placed there by crate::security::CsrfLayer). Calling ChangesetForm::form_tag then emits the hidden _csrf input automatically — no separate CsrfToken parameter needed.

For GET handlers (new/edit), use ChangesetForm::blank and pass csrf.token() from a CsrfToken extractor.

§Example

#[post("/users")]
async fn create(form: ChangesetForm<NewUser>) -> impl IntoResponse {
    match form.into_valid() {
        Ok(user) => { /* persist & redirect */ }
        Err(form) => (StatusCode::UNPROCESSABLE_ENTITY,
                      form.form_tag("/users", "post", html! {
                          (form.text_input("name", "Name"))
                          (form.submit_button("Save"))
                      })).into_response()
    }
}

Fields§

§changeset: Changeset<T>

The validated (or invalid) changeset.

Implementations§

Source§

impl<T> ChangesetForm<T>

Source

pub fn blank(data: T, csrf_token: &str) -> Self

Build a blank form context for GET handlers (new / edit).

Wraps data in a valid Changeset and stores csrf_token so that ChangesetForm::form_tag can emit the hidden input automatically.

#[get("/users/new")]
async fn new_user(csrf: CsrfToken) -> Markup {
    let ctx = ChangesetForm::blank(UserForm::default(), csrf.token());
    ctx.form_tag("/users", "post", html! { (ctx.text_input("name", "Name")) })
}
Source

pub fn without_csrf(data: T) -> Self

Construct a display-only ChangesetForm with no CSRF token.

Use this on GET handlers where CSRF middleware is not active, or when the form will be re-rendered purely for display (e.g. an initial blank form on a page that does not enforce CSRF). form_tag will omit the hidden CSRF input when no token is stored.

Source

pub fn from_changeset(changeset: Changeset<T>) -> Self

Wrap a pre-built Changeset (which may already carry validation errors) in a ChangesetForm without a CSRF token.

Useful in tests and cases where a Changeset was produced externally (e.g. via IntoChangeset) before constructing a form for rendering.

Source

pub fn with_csrf_field(self, field: impl Into<String>) -> Self

Override the CSRF form-field name used by ChangesetForm::form_tag.

Call this when security.csrf.form_field is set to something other than "_csrf" (e.g. "authenticity_token"). The CsrfFormField extension populated by from_request sets this automatically for POST handlers; use this builder on GET handlers that construct a blank form with blank.

Source

pub fn csrf_token(&self) -> Option<&str>

The CSRF token captured from the request, if the CSRF middleware is active.

Source

pub fn into_changeset(self) -> Changeset<T>

Consume and return only the inner Changeset.

Source

pub fn into_valid(self) -> Result<T, Self>

Return Ok(T) if the changeset is valid, Err(self) if not.

The Err branch returns the whole ChangesetForm (with its CSRF token) so the handler can immediately call form.form_tag() to re-render with inline errors.

§Errors

Returns Err(self) when the inner changeset has field-level validation errors.

Source§

impl<T: Serialize> ChangesetForm<T>

Maud rendering methods — emit form HTML with automatic CSRF injection.

Source

pub fn form_tag(&self, action: &str, method: &str, content: Markup) -> Markup

Render a <form> element with the stored CSRF token injected as a hidden input — the field name honours security.csrf.form_field from config, so no developer action is required even for non-default names.

Source

pub fn text_input(&self, field: &str, label: &str) -> Markup

Render a labeled <input type="text"> for field using the stored changeset (value + errors).

Source

pub fn submit_button(&self, label: &str) -> Markup

Render a <button type="submit"> with label.

Methods from Deref<Target = Changeset<T>>§

Source

pub fn is_valid(&self) -> bool

Returns true when there are no field-level errors.

Source

pub fn errors_for(&self, field: &str) -> &[String]

Returns the validation messages for field, or an empty slice.

Source

pub fn data(&self) -> &T

Shared reference to the inner data.

Source

pub fn errors(&self) -> &HashMap<String, Vec<String>>

All field errors as a map (field name → list of messages).

Source

pub fn field_value(&self, field: &str) -> Option<String>

Serialize the value of field from the inner data to a String.

Used by rendering helpers to re-populate <input value="…"> after a failed submission. Returns None for missing or non-scalar fields.

Trait Implementations§

Source§

impl<T> Deref for ChangesetForm<T>

Dereferences to Changeset<T> so all changeset methods are available directly on ChangesetForm<T>form.is_valid(), form.errors_for(…), etc.

Source§

type Target = Changeset<T>

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<S, T> FromRequest<S> for ChangesetForm<T>

Source§

type Rejection = Response<Body>

If the extractor fails it’ll use this “rejection” type. A rejection is a kind of error that can be converted into a response.
Source§

async fn from_request(req: Request, state: &S) -> Result<Self, Self::Rejection>

Perform the extraction.

Auto Trait Implementations§

§

impl<T> Freeze for ChangesetForm<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for ChangesetForm<T>
where T: RefUnwindSafe,

§

impl<T> Send for ChangesetForm<T>
where T: Send,

§

impl<T> Sync for ChangesetForm<T>
where T: Sync,

§

impl<T> Unpin for ChangesetForm<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for ChangesetForm<T>
where T: UnsafeUnpin,

§

impl<T> UnwindSafe for ChangesetForm<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> AggregateExpressionMethods for T

Source§

fn aggregate_distinct(self) -> Self::Output
where Self: DistinctDsl,

DISTINCT modifier for aggregate functions Read more
Source§

fn aggregate_all(self) -> Self::Output
where Self: AllDsl,

ALL modifier for aggregate functions Read more
Source§

fn aggregate_filter<P>(self, f: P) -> Self::Output
where P: AsExpression<Bool>, Self: FilterDsl<<P as AsExpression<Bool>>::Expression>,

Add an aggregate function filter Read more
Source§

fn aggregate_order<O>(self, o: O) -> Self::Output
where Self: OrderAggregateDsl<O>,

Add an aggregate function order Read more
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> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Converts Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Converts Rc<Trait> (where Trait: Downcast) to Rc<Any>, which can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Converts &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Converts &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> DowncastSend for T
where T: Any + Send,

Source§

fn into_any_send(self: Box<T>) -> Box<dyn Any + Send>

Converts Box<Trait> (where Trait: DowncastSend) to Box<dyn Any + Send>, which can then be downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

Source§

fn into_any_sync(self: Box<T>) -> Box<dyn Any + Send + Sync>

Converts Box<Trait> (where Trait: DowncastSync) to Box<dyn Any + Send + Sync>, which can then be downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Send + Sync>

Converts Arc<Trait> (where Trait: DowncastSync) to Arc<Any>, which can then be downcast into Arc<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> IntoSql for T

Source§

fn into_sql<T>(self) -> Self::Expression

Convert self to an expression for Diesel’s query builder. Read more
Source§

fn as_sql<'a, T>(&'a self) -> <&'a Self as AsExpression<T>>::Expression
where &'a Self: AsExpression<T>, T: SqlType + TypedExpressionType,

Convert &self to an expression for Diesel’s query builder. Read more
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: 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: 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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T, Conn> RunQueryDsl<Conn> for T

Source§

fn execute<'conn, 'query>( self, conn: &'conn mut Conn, ) -> <Conn as AsyncConnectionCore>::ExecuteFuture<'conn, 'query>
where Conn: AsyncConnectionCore + Send, Self: ExecuteDsl<Conn> + 'query,

Executes the given command, returning the number of rows affected. Read more
Source§

fn load<'query, 'conn, U>( self, conn: &'conn mut Conn, ) -> AndThen<Self::LoadFuture<'conn>, TryCollect<Self::Stream<'conn>, Vec<U>>>
where U: Send, Conn: AsyncConnectionCore, Self: LoadQuery<'query, Conn, U> + 'query,

Executes the given query, returning a Vec with the returned rows. Read more
Source§

fn load_stream<'conn, 'query, U>( self, conn: &'conn mut Conn, ) -> Self::LoadFuture<'conn>
where Conn: AsyncConnectionCore, U: 'conn, Self: LoadQuery<'query, Conn, U> + 'query,

Executes the given query, returning a [Stream] with the returned rows. Read more
Source§

fn get_result<'query, 'conn, U>( self, conn: &'conn mut Conn, ) -> AndThen<Self::LoadFuture<'conn>, LoadNext<Pin<Box<Self::Stream<'conn>>>>>
where U: Send + 'conn, Conn: AsyncConnectionCore, Self: LoadQuery<'query, Conn, U> + 'query,

Runs the command, and returns the affected row. Read more
Source§

fn get_results<'query, 'conn, U>( self, conn: &'conn mut Conn, ) -> AndThen<Self::LoadFuture<'conn>, TryCollect<Self::Stream<'conn>, Vec<U>>>
where U: Send, Conn: AsyncConnectionCore, Self: LoadQuery<'query, Conn, U> + 'query,

Runs the command, returning an Vec with the affected rows. Read more
Source§

fn first<'query, 'conn, U>( self, conn: &'conn mut Conn, ) -> AndThen<<Self::Output as LoadQuery<'query, Conn, U>>::LoadFuture<'conn>, LoadNext<Pin<Box<<Self::Output as LoadQuery<'query, Conn, U>>::Stream<'conn>>>>>
where U: Send + 'conn, Conn: AsyncConnectionCore, Self: LimitDsl, Self::Output: LoadQuery<'query, Conn, U> + Send + 'query,

Attempts to load a single record. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> Scoped for T
where T: Send + Sync + 'static,

Source§

fn scope(ctx: &PolicyContext) -> ScopeQuery<'_, Self>

Open a deferred ScopeQuery for this type. Resolves the registered scope at .load() time, not here.
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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> WindowExpressionMethods for T

Source§

fn over(self) -> Self::Output
where Self: OverDsl,

Turn a function call into a window function call Read more
Source§

fn window_filter<P>(self, f: P) -> Self::Output
where P: AsExpression<Bool>, Self: FilterDsl<<P as AsExpression<Bool>>::Expression>,

Add a filter to the current window function Read more
Source§

fn partition_by<E>(self, expr: E) -> Self::Output
where Self: PartitionByDsl<E>,

Add a partition clause to the current window function Read more
Source§

fn window_order<E>(self, expr: E) -> Self::Output
where Self: OrderWindowDsl<E>,

Add a order clause to the current window function Read more
Source§

fn frame_by<E>(self, expr: E) -> Self::Output
where Self: FrameDsl<E>,

Add a frame clause to the current window function Read more
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
Source§

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,