macwinnie/twigbundle-form

A Twig Bundle providing some helper templating for forms

1.0.2 2024-01-22 11:15 UTC

This package is auto-updated.

Last update: 2024-04-22 11:48:05 UTC


README

This Twig Bundle allows to reuse some helpful Form elements ... as Twig Macros.

WARNING: as always when working with forms ... the view isn't the (only) place to verify if a user is allowed to send specific information ... if you disable a form element or make it read only in the view, you also want to prove the data sent does not contain that specific value changed!

Data structures

As always, a template is using a dedicated data structure to be transferred into a view ... So the following section is on how the data has to be structured for the usage of the provided macros:

Form

The form macro is using a Bootstrap-DIV-based layout and can be adapted easily. To use it, you'll want to require the \macwinnie\TwigbundleForm\FormExtension class as Twig extension:


Form Element

By default, if none of the following options is selected, we'll get a <input> field rendered by this macro. If

Example usage

This is an example of a Twig template on how to build forms and a single form element:

<h3>Form-Testing</h3>

{% set form_data = {
    "create":
    {
        "action": "/tests/helper/?"
    },
    "buttons":
    [
        {
            "text": "submit",
            "class": "btn btn-primary",
            "name": "submitbutton",
            "value": "submit_val"
        }
    ],
    "rows":
    [
        {
            "name": "text",
            "placeholder": "ph text",
            "type": "text",
            "title": "single line textfield"
        }
    ]
} %}

{% import "form.twig" as forms %}
{% import "formelement.twig" as formelement %}

{{ forms.create( form_data ) }}

<hr/>

{{ formelement.create( form_data.rows[0] ) }}