Пример #1
0
def form_input_text(form: forms.Form,
                    field: BoundField,
                    input_type: str = "text") -> str:
    """Form group with label, input, and errors when form is bound."""
    form_is_bound = form.is_bound
    errors = field.errors
    has_errors = form_is_bound and errors

    return format_html(
        """
    <div class="form-group">
        <label for="{input_id}">{label}</label>
        <input
            type="{input_type}"
            class="form-control {invalid_class}"
            id="{input_id}"
            name="{name}"
            value="{value}"
            aria-describedby="{input_id}-help {input_id}-feedback"
            required>
        <small id="{input_id}-help" class="form-text text-muted">{help_text}</small>
        <div id="{input_id}-feedback" class="invalid-feedback">{errors}</div>
    </div>
    """,
        input_id=field.id_for_label,
        label=field.label,
        input_type=input_type,
        invalid_class="is-invalid" if has_errors else "",
        has_errors=has_errors,
        name=field.html_name,
        value=field.value() or "",
        help_text=field.help_text,
        errors=" ".join(field.errors),
    )
Пример #2
0
def form_input_checkbox(
    form: forms.Form,
    field: BoundField,
    tooltip_src: str = "ui/svg/info-circle-fill.svg",
    placement: str = "right",
) -> str:
    """Form group with label, checkbox input, and tooltip."""
    form_is_bound = form.is_bound
    errors = field.errors
    has_errors = form_is_bound and errors

    return format_html(
        """
    <div class="form-group">
        <div class="form-check">
            <input
            type="checkbox"
            class="form-check-input {invalid_class}"
            id="{input_id}" {checked}/>
            <label class="form-check-label" for="{input_id}">{label}</label>
            <img
                id="{input_id}-tooltip"
                src="{static_url}{tooltip_src}"
                data-toggle="tooltip"
                data-placement="{placement}"
                title="{help_text}" />
        </div>
    </div>
    """,
        input_id=field.id_for_label,
        label=field.label,
        invalid_class="is-invalid" if has_errors else "",
        checked="checked" if form_is_bound and field.value() else "",
        static_url=settings.STATIC_URL,
        tooltip_src=tooltip_src,
        placement=placement,
        help_text=field.help_text,
    )