Helpers & URL access
Access to form values and URL parameters โ typed helpers and whitelisted access.
Live demo
Active route: /formulaires/helpers/rust
ยท
from_url("search"): โ
ยท
cleaned_string("search"): โ
Test by modifying the URL:
POST Helpers โ after is_valid()
// Default value if field is empty
form.get_string("username") // -> String ("" if empty)
form.get_i32("age") // -> i32 (0 by default)
form.get_f64("price") // -> f64 (handles , โ .)
form.get_bool("active") // -> bool (true/1/on โ true)
form.get_uuid("ref") // -> Uuid (Uuid::nil() if empty)
// Option โ None if empty
form.get_option("bio") // -> Option<String>
form.get_option_i32("age") // -> Option<i32>
form.get_option_f64("note") // -> Option<f64>
form.get_option_bool("news") // -> Option<bool>
form.get_option_uuid("id") // -> Option<Uuid>
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.get_naive_date("birthday")
form.get_naive_time("meeting")
form.get_naive_datetime("event_start")
form.get_datetime_utc("created_at")
// Option variants
form.get_option_naive_date("birthday")
form.get_option_naive_datetime("event_start")
form.get_option_datetime_utc("created_at")
Raw URL parameter access โ from Request
// Declared route: "/article/{id}"
let id = request.path_param("id"); // Option<&str>
// Query string: /article/42?page=2
let page = request.from_url("page"); // Option<&str>
cleaned_*() โ whitelisted, typed, all sources
// Priority: POST โ path param โ query param
// None if field is not declared in the form
form.cleaned_string("search") // Option<String>
form.cleaned_i32("page") // Option<i32>
form.cleaned_i64("id") // Option<i64>
form.cleaned_f64("price") // Option<f64> (handles , โ .)
form.cleaned_bool("active") // Option<bool>
form.cleaned_string("is_admin") // None โ unknown field
Real example โ GET search /blog/list?search=rust
// views.rs โ filter articles according to query string
pub async fn blog_list(
mut request: Request,
Prisme(form): Prisme<SearchDemoForm>,
) -> AppResult<Response> {
let search = form.cleaned_string("search");
let mut query = BlogEntity::find();
if let Some(ref term) = search {
query = query.filter(
Condition::any()
.add(Column::Title.contains(term))
.add(Column::Summary.contains(term)),
);
}
// ...
}
// Real example: user search /view-user?username=alice
let username = form.cleaned_string("username").unwrap_or_default();