March Docs

Form

Form — HTML form rendering helpers and ~H template components.

Provides low-level tag builders (form_tag, input_tag, etc.) and higher-level component functions (Input.render, Select.render, etc.) for use from .march.html templates via the <.Form.Input /> syntax.

Component calling convention (self-closing): <.Form.Input name="email" type_="email" label="Email" gate={gate} /> desugars to: Form.Input.render(name: "email", type_: "email", label: "Email", gate: gate)

Wrapper component (with inner content): <.Form.Wrapper conn={conn} action="/login" method="post"> <.Form.Input name="email" type_="email" label="Email" gate={gate} /> <button type="submit">Log in</button> </.Form.Wrapper> desugars to (via lower.march): Form.Wrapper.render(conn: conn, action: "/login", method: "post", render_inner: fn -> ~H"""...""" end)

All component render functions return IOList so they compose inside ~H.

Functions

fnerror_tagerror_tag(error : Option(String)) : String#

Render an error <span> for a field, or empty string if no error.

    Form.error_tag(Depot.Gate.error_for(gate, "email"))
fnfield_tagfield_tag(name : String, label_text : String, input_html : String, error : Option(String)) : String#

Render a complete form field: label + input wrapper + error.

`name` is used as the label's `for` and the input's `id`.
`label_text` is the display label (empty → no label element rendered).
`input_html` is the pre-rendered input element.
`error` is the optional error message.

    Form.field_tag("email", "Email", input_html, Depot.Gate.error_for(gate, "email"))
fnform_tagform_tag(conn : Conn, action : String, method : String, class : String, content : String) : String#

Render a complete <form> tag with CSRF token and inner content.

The ~H desugarer already auto-injects the CSRF token into plain
<form method="post"> tags, so this is only needed when building
forms outside ~H templates.

    Form.form_tag(conn, "/register", "post", "", content)
fninput_taginput_tag(name : String, type_ : String, value : String, attrs : List((String, String))) : String#

Render an <input> element.

    Form.input_tag("email", "email", "[email protected]", [("class", "input")])
fnselect_tagselect_tag(name : String, options : List((String, String)), selected : String, attrs : List((String, String))) : String#

Render a <select> element with <option> elements.

`options` is a list of (label, value) pairs.
`selected` is the currently selected value (or empty string for none).

    Form.select_tag("role", [("Admin", "admin"), ("Member", "member")], "admin", [])
fntextarea_tagtextarea_tag(name : String, value : String, attrs : List((String, String))) : String#

Render a <textarea> element.

    Form.textarea_tag("body", "Hello world", [])