Helpers & URL access

Access to form values and URL parameters — typed helpers for fields, raw access for the URL.

Live demo

Active route: /formulaires/helpers/  ·  get_query("search"):  ·  cleaned::<String>("search"):

Test by modifying the URL: get_query reads the link's raw value, cleaned only reads the fields of a submitted form and so stays empty here.

cleaned() — typed, form fields only
// Option<T>: None if the field is unknown, empty, or doesn't read as T.
// Reads the form's fields only, never the URL.
// T is the Rust type of the field's column:
// cleaned::<i64> on an i32 column is None, whatever the value.
form.cleaned::<String>("username")   // -> Option<String>
form.cleaned::<i32>("age")         // -> Option<i32>
form.cleaned::<f64>("price")       // -> Option<f64>  (handles , → .)
form.cleaned::<bool>("active")      // -> Option<bool>
form.cleaned::<Uuid>("ref")        // -> Option<Uuid>
form.cleaned::<String>("is_admin")  // None — not a field of the form

// Handling default values:
let user = form.cleaned::<String>("user").unwrap_or_default(); // String
let age  = form.cleaned::<i32>("age").unwrap_or(18);           // i32
clear() — clear the form after processing
// Re-render empty form after success (without redirect)
if form.is_valid().await {
    let path = form.cleaned::<String>("image"); // 1. read before clear
    // save...
    form.clear();                               // 2. clear
    context_update!(request => { "upload_form" => &form });
    return request.render(template);            // 3. empty form displayed
}

// With redirect (PRG): clear() unnecessary
// the new GET request creates a fresh instance automatically
Date / time helpers
form.cleaned::<NaiveDate>("birthday")
form.cleaned::<NaiveTime>("meeting")
form.cleaned::<NaiveDateTime>("event_start")
form.cleaned::<DateTime<Utc>>("created_at")

// All return Option<T>: None if unknown, empty or unreadable
Raw URL parameter access — from Request
// Declared route: "/article/{id}"
let id   = request.get_path("id");    // Option<&str>
let id   = request.get_path_as::<Pk>("id"); // Option<Pk>

// Query string: /article/42?page=2
let page = request.get_query("page"); // Option<&str>
Real example — the blog search, sent with GET
// views.rs — the search form of /blog/liste, sent by its own page
pub async fn blog_list(
    mut request: Request,
) -> AppResult<Response> {
    let form: SearchDemoForm = request.form("search");
    // Filled only when this form was sent (its name comes back with it),
    // then validated: cleaned reads a value that went through the rules.
    let form = match ValidationForm::try_form(form, &request).await {
        Ok(validated) => validated.into_form(),
        Err(form) => form,
    };
    let search = form.cleaned::<String>("search");
    let articles = list_articles(&request.engine.db, search.as_deref()).await;
    // ...
}

// /blog/liste?search=rust typed by hand: the form was not sent,
// so search is None and every article is listed.