{# macros/forms.html - Form component macros for APEP. Import with: {% from "apep/macros/forms.html" import input, checkbox, radio, dropdown, date_picker %} Macros: input(name, label, type, allowed_chars, validate, placeholder, value, required, hint, resize) checkbox(name, options, hidden_name) radio(name, options, selected) dropdown(name, label, options, selected, required) date_picker(name, id, format, display, value, required, min_date, max_date, placeholder) CSS dependency: forms.css (loaded via ApepLoader.register("forms.css")) Author: sora7672 #} {% from "apep/macros/helper.html" import alert %} {# Renders a labeled text, email, password input, or resizable textarea with optional validation, character filtering, hint text, and inline error state. When type is "email", allowed_chars is ignored and email format validation is applied automatically regardless of the validate param. When resize is set, a {% else %} {% endif %} {% if hint %}

{{ hint }}

{% endif %} {% endif %} {% endmacro %} {# Renders a group of checkboxes. Each row is fully clickable, not just the label. Optionally writes a comma-joined string of all selected values into a hidden input, useful when the consuming backend expects a single field instead of repeated keys. @param name {string} - base name attribute used for all checkbox inputs @param options {list} - list of {label, value, checked} dicts. value falls back to label when omitted. checked defaults to false when omitted. @param hidden_name {string} - if provided, renders a hidden input with this name that is kept in sync with the selected values via checkbox.js. Optional. #} {% macro checkbox(name, options, hidden_name=None) %} {% if not name %} {{ alert("checkbox: name is required", "Pass a non-empty string as the first argument.") }} {% elif not options %} {{ alert("checkbox: options is required", "Pass a list of {label, value} dicts.") }} {% else %}
{% for option in options %} {% set opt_value = option.value if option.value is defined else option.label %} {% set is_checked = option.checked is defined and option.checked %}
{% endfor %} {% if hidden_name %} {% endif %}
{% endif %} {% endmacro %} {# Renders a group of radio buttons. Each row is fully clickable, not just the label. Each option also gets a hidden input that is enabled only when its radio is selected, so the true value is always submitted even if the browser strips radio names on submit. The selected param takes priority over option.checked when both are present. @param name {string} - input name attribute shared by all radio inputs in the group @param options {list} - list of {label, value, checked} dicts. value falls back to label when omitted. checked defaults to false when omitted. @param selected {string} - pre-selected value. Takes priority over option.checked. Optional. #} {% macro radio(name, options, selected=None) %} {% if not name %} {{ alert("radio: name is required", "Pass a non-empty string as the first argument.") }} {% elif not options %} {{ alert("radio: options is required", "Pass a list of {label, value} dicts.") }} {% else %}
{% for option in options %} {% set opt_value = option.value if option.value is defined else option.label %} {% if selected %} {% set is_checked = (selected == opt_value) %} {% else %} {% set is_checked = (option.checked is defined and option.checked) %} {% endif %}
{% endfor %}
{% endif %} {% endmacro %} {# Renders a labeled native {% for option in options %} {% set opt_value = option.value if option.value is defined else option.label %} {% endfor %} {% endif %} {% endmacro %} {# Renders a styled date picker with a text input, calendar icon button, and an overlay panel containing year/month navigation and a day grid. The overlay supports quick-select grids for year (5x5) and month (3x4). Validation and formatting are handled by date_picker.js. @param name {string} - input name attribute for form submit @param id {string} - explicit id for the input element. Falls back to name when omitted. @param format {string} - date format token string, e.g. "yyyy.mm.dd". Default "yyyy.mm.dd". Shown as placeholder when placeholder is not set. @param display {string} - display mode passed to date_picker.js. Default "readable". @param value {string} - prefilled value matching the chosen format. Default "". @param required {bool} - adds required attribute to the input. Default false. @param min_date {string} - earliest selectable date in the chosen format. Default "". @param max_date {string} - latest selectable date in the chosen format. Default "". @param placeholder {string} - placeholder text. Falls back to the format string when omitted. #} {% macro date_picker( name, id=none, format="yyyy.mm.dd", display="readable", value="", required=false, min_date="", max_date="", placeholder="" ) %} {% set _id = id if id else name %} {% set _placeholder = placeholder if placeholder else format %}
{% endmacro %}