pub struct Form { /* private fields */ }Expand description
HTML form with optional CSRF (on by default) and validator bridge.
Implementations§
Source§impl Form
impl Form
Sourcepub fn builder(name: impl Into<String>) -> FormBuilder
pub fn builder(name: impl Into<String>) -> FormBuilder
Starts a builder named name (also used as CSRF intention id).
Sourcepub const fn is_submitted(&self) -> bool
pub const fn is_submitted(&self) -> bool
Whether the last Self::handle_request bound a submit.
Sourcepub fn file(&self, name: &str) -> Option<&UploadedFile>
pub fn file(&self, name: &str) -> Option<&UploadedFile>
Returns an uploaded file bound to name, when present.
Sourcepub fn take_file(&mut self, name: &str) -> Option<UploadedFile>
pub fn take_file(&mut self, name: &str) -> Option<UploadedFile>
Removes and returns the uploaded file for name, when present.
Sourcepub fn set(&mut self, name: &str, value: impl Into<String>) -> bool
pub fn set(&mut self, name: &str, value: impl Into<String>) -> bool
Sets a field value when the field exists (for example edit prefill).
Returns true when name matched a field.
Sourcepub const fn violations(&self) -> &ConstraintViolationList
pub const fn violations(&self) -> &ConstraintViolationList
Constraint violations from the last Self::validate call.
Sourcepub const fn csrf_enabled(&self) -> bool
pub const fn csrf_enabled(&self) -> bool
Whether CSRF protection is enabled (default true).
Sourcepub fn prepare_csrf(
&mut self,
manager: &dyn CsrfTokenManager,
) -> Result<(), FormError>
pub fn prepare_csrf( &mut self, manager: &dyn CsrfTokenManager, ) -> Result<(), FormError>
Issues a CSRF token when CSRF is enabled (call before render on GET).
§Errors
Returns FormError::Csrf when token generation fails.
Sourcepub fn handle_request(
&mut self,
request: &Request,
manager: &dyn CsrfTokenManager,
) -> Result<FormStatus, FormError>
pub fn handle_request( &mut self, request: &Request, manager: &dyn CsrfTokenManager, ) -> Result<FormStatus, FormError>
Binds POST/PUT/PATCH urlencoded or multipart body and checks CSRF.
Multipart is used when Content-Type is multipart/form-data. File fields
require multipart; size and MIME limits come from each field’s FileOptions.
§Errors
Returns parse, upload, transform, or CSRF errors. Wrong method yields
FormStatus::NotSubmitted without error.
Sourcepub fn validate(&mut self, validator: &dyn Validator) -> bool
pub fn validate(&mut self, validator: &dyn Validator) -> bool
Runs field constraints through validator (defaults to RecursiveValidator when omitted via Self::is_valid).
Sourcepub fn is_valid(&mut self) -> bool
pub fn is_valid(&mut self) -> bool
Validates with RecursiveValidator.
Sourcepub fn render(&self) -> Result<RenderedForm, FormError>
pub fn render(&self) -> Result<RenderedForm, FormError>
Renders a full HTML form (escaped values, CSRF hidden field when enabled).
Adds enctype="multipart/form-data" when the form declares a FieldKind::File field.
§Errors
Returns FormError::Csrf when CSRF is enabled but no token was prepared or bound,
or FormError::Transform when a field transformer rejects the model value.
Auto Trait Implementations§
impl !RefUnwindSafe for Form
impl !UnwindSafe for Form
impl Forget for Form
impl Freeze for Form
impl Move for Form
impl Send for Form
impl Sync for Form
impl Unpin for Form
impl UnsafeUnpin for Form
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
impl<A, B, T> HttpServerConnExec<A, B> for Twhere
B: Body,
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