validation_errors_json

Function validation_errors_json 

Source
pub fn validation_errors_json(errors: &ValidationErrors) -> Value
Expand description

Validation error as JSON for HTMX responses

Returns validation errors in a structured JSON format suitable for HTMX out-of-band swaps or client-side rendering.

ยงExample

use acton_htmx::extractors::{ValidatedForm, validation_errors_json};
use axum::response::{IntoResponse, Json};
use serde::Deserialize;
use validator::Validate;

#[derive(Debug, Deserialize, Validate)]
struct LoginForm {
    #[validate(email)]
    email: String,
}

async fn login(form: Result<ValidatedForm<LoginForm>, acton_htmx::extractors::ValidationError>) -> impl IntoResponse {
    match form {
        Ok(ValidatedForm(form)) => Json(serde_json::json!({"success": true})),
        Err(err) => {
            if let acton_htmx::extractors::ValidationError::Validation(errors) = err {
                return Json(validation_errors_json(&errors));
            }
            Json(serde_json::json!({"error": "Invalid form data"}))
        }
    }
}