Form

Struct Form 

Source
pub struct Form;
Expand description

The <form> element - represents a document section containing interactive controls for submitting information.

§Purpose

The <form> element represents a collection of form-associated elements for gathering user input and submitting data to a server. It provides the context for form controls, handles submission, and defines how data should be encoded and transmitted.

§Content Categories

  • Flow Content
  • Palpable Content

§Permitted Content Model

  • Flow content (but no nested <form> elements)

§Common Use Cases

  • User registration and login forms
  • Search interfaces
  • Contact and feedback forms
  • E-commerce checkout processes
  • Survey and questionnaire forms

§Key Attributes

  • action: URL where form data is sent
  • method: HTTP method for submission (“get” or “post”)
  • enctype: Encoding type for form data (“application/x-www-form-urlencoded”, “multipart/form-data”, “text/plain”)
  • name: Name of the form
  • target: Browsing context for response (“_self”, “_blank”, “_parent”, “_top”)
  • novalidate: Disable built-in validation
  • autocomplete: Enable/disable autocomplete (“on” or “off”)
  • accept-charset: Character encodings for submission

§Example

<!-- Basic login form -->
<form action="/login" method="post">
  <label for="username">Username:</label>
  <input type="text" id="username" name="username" required>
   
  <label for="password">Password:</label>
  <input type="password" id="password" name="password" required>
   
  <button type="submit">Log In</button>
</form>

<!-- Search form with GET -->
<form action="/search" method="get" role="search">
  <label for="q">Search:</label>
  <input type="search" id="q" name="q" placeholder="Enter search terms">
  <button type="submit">Search</button>
</form>

<!-- File upload form -->
<form action="/upload" method="post" enctype="multipart/form-data">
  <label for="file">Choose file:</label>
  <input type="file" id="file" name="file" required>
  <button type="submit">Upload</button>
</form>

<!-- Form with validation disabled -->
<form action="/submit" method="post" novalidate>
  <input type="email" name="email">
  <button type="submit">Submit</button>
</form>

<!-- Form targeting new window -->
<form action="/external" method="post" target="_blank">
  <input type="text" name="data">
  <button type="submit">Open in New Tab</button>
</form>

§Accessibility

  • Use <label> elements for all form controls
  • Group related fields with <fieldset> and <legend>
  • Provide clear error messages near relevant fields
  • Ensure logical tab order through form fields
  • Use autocomplete attributes appropriately
  • Add aria-describedby for additional instructions

§WHATWG Specification

Trait Implementations§

Source§

impl HtmlElement for Form

Source§

const TAG: &'static str = "form"

The HTML tag name (e.g., “div”, “span”, “img”).
Source§

const VOID: bool = false

Whether this is a void element (self-closing, no children allowed).
Source§

impl<T: FlowContent> CanContain<T> for Form

Source§

impl FlowContent for Form

Source§

impl PalpableContent for Form

Auto Trait Implementations§

§

impl Freeze for Form

§

impl RefUnwindSafe for Form

§

impl Send for Form

§

impl Sync for Form

§

impl Unpin for Form

§

impl UnwindSafe for Form

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

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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