Templates

Forms, context & pitfalls

Form error handling

Automatic display (via {% form.xxx %})

When using {% form.signup_form %}, validation errors are automatically rendered under each relevant field.

Manual display of global errors

{% if signup_form.errors %}
    <div class="alert alert-warning">
        <ul>
            {% for field_name, error_msg in signup_form.errors %}
                <li><strong>{{ field_name }}:</strong> {{ error_msg }}</li>
            {% endfor %}
        </ul>
    </div>
{% endif %}

Complete example: page with a form

// Rust handler
pub async fn signup(mut request: Request) -> AppResult<Response> {
    let form: RegisterForm = request.form();

    let mut validated = match ValidationForm::try_new(form, &request).await {
        Ok(validated) => validated,
        Err(form) => {
            // GET (nothing submitted): blank form, no flash.
            // Submitted but invalid: re-render with the error flash.
            if request.method.is_safe() {
                context_update!(request => {
                    "title" => "Sign Up",
                    "signup_form" => &form,
                });
            } else {
                context_update!(request => {
                    "title" => "Validation Error",
                    "signup_form" => &form,
                    "messages" => flash_now!(error => "Please fix the errors"),
                });
            }
            return request.render("signup.html");
        }
    };

    let user = validated.save(&request.engine.db).await.map_err(|err| {
        validated.database_error(&err);
        AppError::from(err)
    })?;
    success!(request.notices => format!("Welcome {}!", user.username));
    Ok(Redirect::to("/").into_response())
}
{% extends "base.html" %}

{% block title %}{{ title }}{% endblock %}

{% block content %}
    <h1>{{ title }}</h1>
    {% messages %}
    <form method="post" action="/signup">
        {% form.signup_form %}
        <button type="submit" class="btn btn-primary">Sign up</button>
    </form>
{% endblock %}

Auto-injected variables

These variables are automatically available in all templates:

VariableTypeDescription
csrf_tokenStringSession CSRF token
csp_nonceStringCSP nonce for inline scripts/styles
messagesVec<FlashMessage>Flash messages from the previous session
debugboolDebug mode status

Common pitfall: variable name collision

When using {% form.user %}, the variable user must be a form (extracted via request.form()):

// ❌ ERROR: "user" is a SeaORM Model, not a form
context_update!(request => {
    "user" => &db_user,  // the form filter will crash!
});

// ✅ CORRECT: separate form and DB entity
context_update!(request => {
    "user" => &form,           // form → {% form.user %} works
    "found_user" => &db_user,  // Model → {{ found_user.email }}
});