Forms

Typed conversion helpers

Form values are stored as String. Use the cleaned_* typed helpers to convert them securely (whitelisted) and with proper typing.

Methods on the RuniqueForm trait. They are whitelisted (undeclared field → None) and cover sources by priority: POST > Route parameter > Query string.

Basics and Numerics

self.cleaned::<String>("username")      // Option<String> (None if empty)
self.cleaned::<i32>("age")              // Option<i32>
self.cleaned::<i64>("count")            // Option<i64>
self.cleaned::<u32>("quantity")         // Option<u32>
self.cleaned::<u64>("id")               // Option<u64>
self.cleaned::<f32>("ratio")            // Option<f32> (handles , → .)
self.cleaned::<f64>("price")            // Option<f64> (handles , → .)
self.cleaned::<bool>("active")          // Option<bool> (true/1/on → true)

Date / Time / UUID

self.cleaned::<NaiveDate>("birthday")           // Option<NaiveDate> (YYYY-MM-DD format)
self.cleaned::<NaiveTime>("meeting_time")       // Option<NaiveTime> (HH:MM format)
self.cleaned::<NaiveDateTime>("event_start")    // Option<NaiveDateTime> (YYYY-MM-DDTHH:MM)
self.cleaned::<DateTime<Utc>>("created_at")       // Option<DateTime<Utc>> (RFC3339)

self.cleaned::<Uuid>("external_id")              // Option<Uuid>

Enums

self.cleaned_enum::<Status>("status")         // Option<Status> — text or integer enum
self.cleaned_enums::<Genre>("genres")         // Vec<Genre> — list field (checkbox / multichoice)

cleaned_enum takes any enum generated by model!{}, text or integer (i8 to i64). cleaned_enums reads the checked values of a list field, in the order submitted; it returns an empty Vec when nothing is checked. A value that isn't a variant is left out (and logged).

💡 Float readings (cleaned::<f32>, cleaned::<f64>) automatically convert commas to dots (19,99 → 19.99) to simplify input.

Security Note: All cleaned_* variants return Option. For a default value: .unwrap_or_default() or .unwrap_or(0).


Usage in `save()`

In your save() methods, exclusively use cleaned_* to extract data:

impl RegisterForm {
    pub async fn save(&self, db: &DatabaseConnection) -> Result<users::Model, DbErr> {
        let model = users::ActiveModel {
            username: Set(self.cleaned::<String>("username").unwrap_or_default()),
            email: Set(self.cleaned::<String>("email").unwrap_or_default()),
            age: Set(self.cleaned::<i32>("age").unwrap_or(0)),
            website: Set(self.cleaned::<String>("website")), // Option<String>
            ..Default::default()
        };
        model.insert(db).await
    }
}

URL parameter access

From Request (Raw access)

// Route parameter: /article/{id}
let id = request.get_path("id");         // Option<&str>

// Query string parameter: ?page=2
let page = request.get_query("page");    // Option<&str>

From the form (Recommended)

The cleaned_* variants automatically merge data from the request body and the URL.

Example — pre-fill a field from the URL (GET /edit?title=My+Article):

if request.method.is_safe() {
    if let Some(t) = form.cleaned::<String>("title") {
        form.forms_mut().add_value("title", &t);
    }
}

← RuniqueForm trait | Field types →