config: added custom html insertion and separated name and title
This commit is contained in:
@@ -18,13 +18,17 @@ use tower_http::cors::{Any, CorsLayer};
|
|||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
struct FieldDef {
|
struct FieldDef {
|
||||||
name: String,
|
name: String,
|
||||||
|
title: String,
|
||||||
description: String,
|
description: String,
|
||||||
answer_type: String,
|
answer_type: String,
|
||||||
|
html_before: Option<String>,
|
||||||
|
html_after: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
struct AppConfig {
|
struct AppConfig {
|
||||||
json_output: String,
|
json_output: String,
|
||||||
|
submit_button: String,
|
||||||
fields: Vec<FieldDef>,
|
fields: Vec<FieldDef>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -111,11 +115,13 @@ async fn render_form(State(state): State<AppState>) -> impl IntoResponse {
|
|||||||
struct FormTemplate<'a> {
|
struct FormTemplate<'a> {
|
||||||
fields: &'a [FieldDef],
|
fields: &'a [FieldDef],
|
||||||
lang: &'a str,
|
lang: &'a str,
|
||||||
|
submit_button: &'a str,
|
||||||
}
|
}
|
||||||
|
|
||||||
let tmpl = FormTemplate {
|
let tmpl = FormTemplate {
|
||||||
fields: &state.cfg.fields,
|
fields: &state.cfg.fields,
|
||||||
lang: "en",
|
lang: "en",
|
||||||
|
submit_button: &state.cfg.submit_button,
|
||||||
};
|
};
|
||||||
match tmpl.render() {
|
match tmpl.render() {
|
||||||
Ok(html) => Html(html).into_response(),
|
Ok(html) => Html(html).into_response(),
|
||||||
|
|||||||
+8
-3
@@ -3,11 +3,13 @@
|
|||||||
{% block title %}Form{% endblock %}
|
{% block title %}Form{% endblock %}
|
||||||
|
|
||||||
{% block content %}
|
{% block content %}
|
||||||
<h1>Dynamic Form</h1>
|
|
||||||
<form action="/submit" method="POST" autocomplete="off">
|
<form action="/submit" method="POST" autocomplete="off">
|
||||||
{% for f in fields %}
|
{% for f in fields %}
|
||||||
|
{% if f.html_before.is_some() %}
|
||||||
|
{{ f.html_before.as_ref().unwrap() | safe }}
|
||||||
|
{% endif %}
|
||||||
<div class="field">
|
<div class="field">
|
||||||
<label for="{{ f.name }}">{{ f.description }}</label>
|
<label>{{ f.title }}</label>
|
||||||
<div class="desc">{{ f.description }}</div>
|
<div class="desc">{{ f.description }}</div>
|
||||||
{% if f.answer_type == "textarea" %}
|
{% if f.answer_type == "textarea" %}
|
||||||
<textarea id="{{ f.name }}" name="{{ f.name }}" rows="5"></textarea>
|
<textarea id="{{ f.name }}" name="{{ f.name }}" rows="5"></textarea>
|
||||||
@@ -17,8 +19,11 @@
|
|||||||
<input id="{{ f.name }}" name="{{ f.name }}" type="{{ f.answer_type }}">
|
<input id="{{ f.name }}" name="{{ f.name }}" type="{{ f.answer_type }}">
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</div>
|
</div>
|
||||||
|
{% if f.html_after.is_some() %}
|
||||||
|
{{ f.html_after.as_ref().unwrap() | safe }}
|
||||||
|
{% endif %}
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
<div><button type="submit">Submit</button></div>
|
<div><button type="submit">{{ submit_button }}</button></div>
|
||||||
</form>
|
</form>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user