rustango_cms/forms/
block.rs1use crate::block::{Block, BlockError, BlockField, BlockFieldMeta, BlockRenderCtx};
16use crate::widget::WidgetKind;
17
18#[derive(Default)]
20pub struct FormBlock;
21
22impl Block for FormBlock {
23 fn type_name(&self) -> &'static str {
24 "form"
25 }
26
27 fn verbose_name(&self) -> &'static str {
28 "Form"
29 }
30
31 fn icon(&self) -> Option<&'static str> {
32 Some("dynamic_form")
33 }
34
35 fn group(&self) -> Option<&'static str> {
36 Some("Forms")
37 }
38
39 fn description(&self) -> Option<&'static str> {
40 Some("Embed a reusable form. Optionally override its success message/redirect.")
41 }
42
43 fn fields(&self) -> Vec<BlockField> {
44 vec![
45 BlockField::widget("form", "Form", WidgetKind::SnippetChooser)
48 .required()
49 .with_meta(BlockFieldMeta::none().chooser_filter("form"))
50 .with_help("The form to show (made under Forms)."),
51 BlockField::widget(
52 "success_message",
53 "Success message override",
54 WidgetKind::Text,
55 )
56 .with_help("Optional — shown after submit instead of the form's default."),
57 BlockField::widget(
60 "success_redirect_url",
61 "Success redirect override",
62 WidgetKind::Text,
63 )
64 .with_help("Optional — a page on this site to open after submit instead of showing a message, e.g. /thank-you."),
65 ]
66 }
67
68 fn render(
69 &self,
70 value: &serde_json::Value,
71 _ctx: &BlockRenderCtx,
72 ) -> Result<String, BlockError> {
73 let obj = value.as_object();
74 let schema_val = obj.and_then(|o| o.get("_schema"));
75 let form_id = obj
76 .and_then(|o| o.get("_form_id"))
77 .and_then(serde_json::Value::as_i64)
78 .unwrap_or(0);
79 let embed = obj
80 .and_then(|o| o.get("_embed"))
81 .and_then(serde_json::Value::as_str)
82 .unwrap_or("");
83 match schema_val {
84 Some(s) => {
85 let mut form = crate::forms::schema::parse(s).unwrap_or_default();
86 if let Some(msg) = obj
88 .and_then(|o| o.get("success_message"))
89 .and_then(serde_json::Value::as_str)
90 .filter(|m| !m.trim().is_empty())
91 {
92 form.settings.success_message = msg.to_owned();
93 }
94 Ok(crate::forms::render::render_form_html(
95 &form, form_id, embed,
96 ))
97 }
98 None => {
99 let slug = obj
100 .and_then(|o| o.get("form"))
101 .and_then(serde_json::Value::as_str)
102 .unwrap_or("");
103 Ok(format!(
104 "<div class=\"rcms-form-missing\">Form <code>{}</code> not found.</div>",
105 crate::forms::render::html_escape(slug)
106 ))
107 }
108 }
109 }
110}
111
112crate::register_block!(FormBlock);
113
114#[cfg(test)]
115mod tests {
116 use super::*;
117
118 #[test]
119 fn the_placement_success_message_replaces_the_forms() {
120 let tera = tera::Tera::default();
121 let ctx = BlockRenderCtx::new(&tera);
122 let schema = serde_json::json!({ "settings": { "success_message": "Form default" }, "pages": [] });
123 let with = serde_json::json!({ "_schema": schema, "_form_id": 3, "_embed": "b1", "success_message": "Placement thanks" });
124 let html = FormBlock.render(&with, &ctx).expect("render");
125 assert!(html.contains(">Placement thanks</div>"), "{html}");
126 let without = serde_json::json!({ "_schema": schema, "_form_id": 3, "_embed": "b1", "success_message": " " });
127 let html = FormBlock.render(&without, &ctx).expect("render");
128 assert!(html.contains(">Form default</div>"), "{html}");
129 }
130}