Forms
Documentation / Forms / Field types

Field types

TextField — Text fields

TextField supports 6 special formats via the SpecialFormat enum:

// Plain text
form.field(&TextField::text("username").label("Name").required());

// Email — validated via `validator::ValidateEmail`
form.field(&TextField::email("email").label("Email").required());

// URL — absolute, `http` or `https` only
form.field(&TextField::url("website").label("Website"));

// Registration / password change form — automatic hashing in finalize()
form.field(
    &TextField::password("password")
        .label("Password")
        .required()
        .min_length(8, "Min 8 characters"),
);

// Login form — .no_hash() is required
// Without .no_hash(), finalize() hashes the submitted password before comparison
// → verify(hash, stored_hash) always fails silently
form.field(
    &TextField::password("password")
        .label("Password")
        .no_hash()
        .required(),
);

// Textarea
form.field(&TextField::textarea("summary").label("Summary"));

// RichText — hand-written HTML, sanitized before validation (see below)
form.field(&TextField::richtext("content").label("Content"));

// Phone — digits/spaces/dashes/parentheses, leading '+' only, 7 to 15 digits
form.field(&TextField::phone("phone").label("Phone"));

Builder options:

TextField::text("name")
    .label("My field")              // Display label
    .placeholder("Enter...")        // Placeholder
    .required()                     // Required (default message)
    .min_length(3, "Too short")     // Min length with message
    .max_length(100, "Too long")    // Max length with message
    .readonly("Read-only")          // Read-only
    .disabled("Disabled")           // Disabled

Automatic behavior per format:

FormatValidationTransformation
Emailvalidator::ValidateEmailLowercased
UrlAbsolute http or https URL, read the way a browser reads it (javascript:, data:, file:, ftp:, mailto: and relative URLs are refused)—
PasswordStandardAuto hash in finalize() if config is Auto and no .no_hash(), value cleared on render()
RichTextStandardXSS sanitization (sanitize()) before validation
CsrfSession token—
Text (text, textarea)StandardHTML tags removed, the rest kept as typed
PhoneAllowed chars (digits/spaces/-/()), leading + only, 7 to 15 digits—

The richtext field expects hand-written HTML

Runique ships no visual editor. The field is rendered as a text area (<textarea class="richtext-editor">) and its value is HTML, sanitized on every save. A JavaScript editor of your choice can hook onto the richtext-editor class.

Text without tags is kept as it is: its line breaks produce neither paragraphs nor lists, and the browser shows it as a single block. To structure it, write the tags:

<h2>Title</h2>
<p>A paragraph with an <strong>important word</strong> and a <a href="https://example.com" target="_blank">link</a>.</p>
<ul>
<li>First point</li>
<li>Second point</li>
</ul>

What the sanitizer keeps. A tag missing from this list is removed and its text is kept; the content of a <script> or <style> tag is deleted:

Allowed
Textp, br, hr, strong, em, b, i, u, del, s, sub, sup, code, a, img
Headingsh1 to h6
Blocks and listsul, ol, li, blockquote, pre
Tablestable, thead, tbody, tr, th, td
Attributesa: href, title, target · img: src, alt, title · code: class
  • Link and image addresses must be absolute, in http or https: a relative address or another scheme (javascript:, data:) is removed.
  • target only accepts _blank, and rel="noopener noreferrer" is added to every link.
  • No style attribute, no event handler (onclick…), no HTML comment.

In a template, the value is shown with {{ article.content | safe }}: without | safe, Tera escapes the tags and the visitor sees them as text. Use | safe on a richtext field only, never on an ordinary text field.

Password utilities:

Hashing and verification are delegated to PasswordConfig, initialized at startup via password_init():

use runique::prelude::{hash, verify};

// Hash manually (e.g. account creation outside a form)
let hashed = hash("my_password")?;

// Verify a plain password against a stored hash (e.g. login)
let ok = verify("plain_pwd", &user.password);
if !ok {
    // incorrect password
}

A value typed by the visitor is always hashed by finalize(), even when it looks like a hash: stored as is, it would carry cost parameters of the visitor's choosing. Only a value set by the code (add_value, set_value) and already recognised as a hash (Argon2 $argon2id$/$argon2i$/$argon2d$, bcrypt $2, scrypt $scrypt$) is kept as is.

In a login form, finalize() would therefore hash the typed password before the comparison, and signing in would always fail. That's why .no_hash() is required on a login form's password field. Validate with try_form, then pass the password to authenticate_user (which checks it in constant time, even for an unknown name).

See → Password configuration for every mode (Auto, Manual, Delegated, Custom) and the setup in main.rs.


NumericField — Numeric fields

5 variants via the NumericConfig enum:

// Integer with bounds
form.field(
    &NumericField::integer("age")
        .label("Age")
        .min(0.0, "Min 0")
        .max(150.0, "Max 150"),
);

// Float number
form.field(&NumericField::float("price").label("Price"));

// Decimal with precision
form.field(
    &NumericField::decimal("amount")
        .label("Amount")
        .digits(2, 4),  // min 2, max 4 digits after the decimal separator
);

// Percentage (0–100 by default)
form.field(&NumericField::percent("rate").label("Rate"));

// Range slider with min, max, default value
form.field(
    &NumericField::range("volume", 0.0, 100.0, 50.0)
        .label("Volume")
        .step(5.0),
);

Options: .min(val, msg), .max(val, msg), .min_i64(val, msg) / .max_i64(val, msg) (exact integer bound, useful past 2^53), .step(val) (range type only), .digits(min, max), .label(l), .placeholder(p)


BooleanField — Checkboxes / Single radio

// Simple checkbox
form.field(
    &BooleanField::new("accept_terms")
        .label("I accept the terms")
        .required(),
);

Note on .required(): on a BooleanField, .required() means "must be checked" (terms, consent): an unchecked box is an error. A box always answers (true or false), so a NOT NULL column needs no required: in the DSL, active: bool is enough, and terms: bool [required] forces the check. required with default: true is a compile error: a box checked in advance isn't consent. The box only accepts the value it sends ("true", or the one set with .checked_value(..)): any other value is an error.

// Single radio (yes/no)
form.field(&BooleanField::radio("newsletter").label("Newsletter"));

// Pre-checked
form.field(&BooleanField::new("remember_me").label("Remember me").checked());

ChoiceField — Select / Dropdown

use runique::forms::fields::choice::ChoiceOption;

let choices = vec![
    ChoiceOption::new("fr", "France"),
    ChoiceOption::new("be", "Belgium"),
    ChoiceOption::new("ch", "Switzerland"),
];

// Single select
form.field(
    &ChoiceField::new("country")
        .label("Country")
        .choices(choices.clone())
        .required(),
);

// Multiple select
form.field(
    &ChoiceField::new("languages")
        .label("Languages")
        .choices(choices)
        .multiple(),
);

Validation automatically checks that the submitted value is among the declared choices.


RadioField — Radio buttons

form.field(
    &RadioField::new("gender")
        .label("Gender")
        .choices(vec![
            ChoiceOption::new("m", "Male"),
            ChoiceOption::new("f", "Female"),
            ChoiceOption::new("o", "Other"),
        ])
        .required(),
);

CheckboxField — Multiple checkboxes

form.field(
    &CheckboxField::new("hobbies")
        .label("Hobbies")
        .choices(vec![
            ChoiceOption::new("sport", "Sports"),
            ChoiceOption::new("music", "Music"),
            ChoiceOption::new("reading", "Reading"),
        ]),
);

Submitted values are in the form "val1,val2,val3". Validation checks that each value exists in the choices.


DateField, TimeField, DateTimeField — Date / Time

use chrono::NaiveDate;

// Date (format: YYYY-MM-DD)
form.field(
    &DateField::new("birthday")
        .label("Birth date")
        .min(NaiveDate::from_ymd_opt(1900, 1, 1).unwrap(), "Too old")
        .max(NaiveDate::from_ymd_opt(2010, 12, 31).unwrap(), "Too recent"),
);

// Time (format: HH:MM)
form.field(&TimeField::new("meeting_time").label("Meeting time"));

// Date + Time (format: YYYY-MM-DDTHH:MM)
form.field(&DateTimeField::new("event_start").label("Event start"));

DurationField — Duration

form.field(
    &DurationField::new("timeout")
        .label("Delay (seconds)")
        .min_seconds(60, "Minimum 1 minute")
        .max_seconds(3600, "Maximum 1 hour"),
);

FileField and MediaField — File uploads

Two fields share the same upload mechanism (staging folder, move between the write and the commit, size, count, destination):

  • MediaField — images, audio and video: MediaField::image(), ::audio(), ::video(). The content of each file is checked (its signature), not only its extension: a text file renamed .mp4 is refused.
  • FileField — documents and other files: FileField::document(), ::any(). Only the extension, size and count are checked.
ConstructorExtensions accepted by defaultContent checked
MediaField::imagejpg, jpeg, png, gif, webp, avifsignature + dimensions (max_dimensions)
MediaField::audiomp3, ogg, oga, wav, m4a, flac, aacsignature (MP3, Ogg, WAV, FLAC, MP4/M4A, AAC)
MediaField::videomp4, webm, mov, ogvsignature (MP4/MOV, WebM, Ogg)
FileField::documentpdf, doc, docx, txt, odtno
FileField::anyall but active extensions (.svg, .html, .js…)no

The default maximum size is 10 MB for all; for audio and video, set .max_size(). Two global limits apply too, and win over .max_size(): RUNIQUE_MAX_UPLOAD_MB per file and RUNIQUE_MAX_BODY_MB for the whole request, all files together (100 MB by default each). Values, combinations and what happens when exceeded: Assets & Media.

// Image with full constraints — explicit directory
form.field(
    &MediaField::image("avatar")
        .label("Profile picture")
        .upload_to("uploads/avatars")   // → {MEDIA_ROOT}/uploads/avatars/
        .max_size(FileSize::mb(5))
        .max_files(1)
        .max_dimensions(1920, 1080)
        .allowed_extensions(vec!["png", "jpg", "jpeg", "webp", "avif"]),
);

// Image — automatic directory from MEDIA_ROOT (.env)
// Files go to {MEDIA_ROOT}/{field_name}/  e.g. media/photo/
form.field(
    &MediaField::image("photo")
        .label("Photo")
        .upload_to_env()
        .max_size(FileSize::mb(5)),
);

// Without upload_to — files stored directly in MEDIA_ROOT
form.field(
    &MediaField::image("image")
        .label("Image")
        .max_size(FileSize::mb(5)),
);

// Audio and video: the content is checked as for an image
form.field(
    &MediaField::audio("podcast")
        .label("Episode")
        .upload_to("podcasts")
        .max_size(FileSize::mb(50)),
);
form.field(
    &MediaField::video("trailer")
        .label("Trailer")
        .upload_to("videos")
        .max_size(FileSize::mb(200)), // over 100 MB: RUNIQUE_MAX_UPLOAD_MB=200 in the .env
);

// Document
form.field(
    &FileField::document("cv")
        .label("Resume")
        .upload_to("uploads/cv")
        .max_size(FileSize::mb(10)),
);

// Any extension except those a browser runs (.svg, .html, .js…). Content is not checked:
// what the file is, and how it is served, is the application's responsibility.
form.field(
    &FileField::any("attachments")
        .label("Attachments")
        .max_files(5),
);

Note: .max_size() on the field sets its ceiling. For a form taken from a model (#[form(schema = …)], the form generated by model!{}), the model's max_size is applied again after customize(): the form can tighten it, never exceed it. For a manual form, it is the field's .max_size() value; form.max_size(name, size) in the handler can only go below it (see RuniqueForm trait).

File destination:

MethodDestination
.upload_to("uploads/images"){MEDIA_ROOT}/uploads/images/ (relative to MEDIA_ROOT)
.upload_to_env(){MEDIA_ROOT}/{field_name}/
(none)MEDIA_ROOT directly (no subdirectory)

finalize() prepares the relative path but keeps the file in its temporary staging directory, under MEDIA_ROOT, named {uuid}.extension. With ValidationForm::save, Runique writes the row, moves the file to its destination, then commits the transaction. Until the row is committed no one else sees it: no reader meets a path whose file isn't in place yet. A refused write (value already taken, foreign key…) leaves the file in staging, removed at the end of the request; only a failing commit itself takes back the file already moved. The destination must be relative to MEDIA_ROOT and cannot contain . or .. components, Windows separators, or a volume prefix. The directory is created automatically if it does not already exist.

The file of a deleted row. Deleting a row, from your code or from the admin, doesn't delete its files: Runique can't know whether you must keep them (archive, legal duty) or remove them (right to erasure). If the file has to go, read its path before the deletion and remove it once the deletion is committed.

The replaced file. By default, Runique doesn't delete the file a new upload replaces: you may still need it (history, archive). To have it deleted, declare the field with .replace():

MediaField::image("avatar").upload_to("avatars").replace()

When the form edits a row that already had a file, the old one is deleted once the new one is saved and the write committed. What bounds .replace():

  • a single file: no effect on a field taking several (max_files above 1), where nothing tells which one a new file replaces;
  • only after a new upload: a form sent back without a new file keeps its own;
  • only if the column is really written: a locked field, or a column the account may not change (admin), keeps its file;
  • never blocking: if the old file is missing or can't be deleted, the request still succeeds, and the failure is traced;
  • under MEDIA_ROOT only: a path that would leave it is never deleted.

Without save. A form without a model (a plain file upload) or a hand-written save handles the file itself: form.forms_mut().publish_uploads().await inside your transaction, after the write and before the commit(), then complete_uploads().await once committed, or rollback_uploads().await if the commit fails. With no database write, publish_uploads() then complete_uploads() is enough.

Inside the handler's transaction (request.db_atomic()), the file is moved at the save. If the handler leaves without commit(), the row is rolled back but the file stays on disk, orphaned.

Current limit: the disk takes no part in the transaction. A hard stop of the process between moving the file and the database commit, or a commit failure that can't be caught, can leave an orphan file under MEDIA_ROOT — never a row pointing at a missing file.

Security: extensions a browser opens as a page of the site are always rejected: .svg, .svgz, .html, .htm, .xhtml, .xht, .shtml, .xml, .js, .mjs (XSS risk), whatever the field's list. FileField::any accepts every other extension and does not read the content: the security of that field is the application's responsibility. For a MediaField, validation checks the real format by reading the file's signature (not just the extension): image, audio or video. With max_dimensions, the image crate measures the file: if the measurement fails, the file is refused, for every format. Without that measurement, the width and height limits would not apply. Empty submissions (no file selected) are handled correctly — the required constraint works as expected.

Associated JS files

fn register_fields(form: &mut Forms) {
    // ... fields ...
    form.add_js(&["js/my_script.js", "js/other.js"]);
}

JS files are automatically included in the form's HTML rendering, with the request's CSP nonce and their SRI hash.


ColorField — Color picker

form.field(
    &ColorField::new("theme_color")
        .label("Theme color")
        .default_color("#3498db"),  // Validates #RGB or #RRGGBB format
);

SlugField — URL-friendly slug

form.field(
    &SlugField::new("slug")
        .label("Slug")
        .placeholder("my-url-article")
        .allow_unicode(),  // Optional: allow unicode characters
);

Validation: letters, digits, hyphens, underscores only. Cannot start or end with a hyphen.


UUIDField

form.field(
    &UUIDField::new("external_id")
        .label("External ID")
        .placeholder("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"),
);

JSONField — Textarea with JSON validation

form.field(
    &JSONField::new("metadata")
        .label("Metadata")
        .placeholder(r#"{"key": "value"}"#)
        .rows(10),  // Number of textarea rows
);

IPAddressField — IP address

// IPv4 + IPv6
form.field(&IPAddressField::new("server_ip").label("Server IP"));

// IPv4 only
form.field(&IPAddressField::new("gateway").label("Gateway").ipv4_only());

// IPv6 only
form.field(&IPAddressField::new("ipv6").label("IPv6 address").ipv6_only());

HiddenField — Hidden field

An invisible field in the HTML form (<input type="hidden">). Main use: pass technical data without showing it to the user.

// Generic hidden field (e.g. linked entity ID)
form.field(
    &HiddenField::new("entity_id")
        .label("Entity ID"),
);

The CSRF field is emitted naturally by every RuniqueForm (req.form("name") / form_fields.html / {% form.xxx %}) — no need for HiddenField::new_csrf() or {% csrf %} in the template. {% csrf %} is only for a hand-written HTML <form>, outside RuniqueForm.


Field types summary

StructConstructorsSpecial validation
TextFieldtext(), email(), url(), password(), textarea(), richtext(), phone()Email via validator, http/https URLs only, hashing per the PasswordConfig, XSS sanitization, .rows(n)
NumericFieldinteger(), float(), decimal(), percent(), range()Min/max bounds, decimal precision, .step(n) (range only)
BooleanFieldnew(), radio()required = must be checked; only its value is accepted
ChoiceFieldnew() + .multiple()Value must be in declared choices
RadioFieldnew()Value must be in declared choices
CheckboxFieldnew()All values must be in choices
DateFieldnew()YYYY-MM-DD format, min/max bounds
TimeFieldnew()HH:MM format, min/max bounds
DateTimeFieldnew()YYYY-MM-DDTHH:MM format, min/max bounds
DurationFieldnew()Seconds, min/max bounds
MediaFieldimage(), audio(), video()Extensions, size, content signature, dimensions (image), active extensions refused
FileFielddocument(), any()Extensions, size, active extensions refused (.svg, .html, .js…)
ColorFieldnew()#RRGGBB or #RGB format
SlugFieldnew()ASCII/unicode, no hyphen at start/end
UUIDFieldnew()Valid UUID format
JSONFieldnew()Valid JSON via serde_json, .rows(n)
IPAddressFieldnew() + .ipv4_only() / .ipv6_only()IPv4/IPv6 via std::net::IpAddr
HiddenFieldnew(), new_csrf()CSRF token validation if name == "csrf_token"

← Typed conversion helpers | Database errors →

Why the use of AI?