Forms

Introduction

The form plugin function allows you to initialize, configure, and display a form builder inline within a view.

Displaying Forms

The easiest way to display a form is to use the form function and specify the namespace and stream slug.

The render method will trigger display but it is optional as __toString will result in the same thing.

{{ form('books', 'reviews').render()|raw }}

You may want to take more control and set the form to a variable and use it.

{% set form = form('books', 'reviews').get() %}

{{ form.open|raw }}

    {% for field in form.fields %}
    <div class="{{ field.input_name }}-wrapper">
        {{ field.render()|raw
    </div>
    {% endfor %}

    {{ form.actions|raw }}

{{ form.close|raw }}

Binding Form Builders

The form function makes use of a generic form builder class by default. Often times, however, you want to build and use a specific form builder. The easiest way to use said builder in the form function is to bind it to the service container.

You can define a form builder binding within a service provider.

Service Providers{.link} Form Builders{.link}

protected $bindings = [
    'employment_application_form' => \Anomaly\ExampleModule\Applications\Form\PublicApplicationFormBuilder::class
];

After binding the form builder you can use it by specifying the bound key.

{{ form('employment_application').render()|raw }}

Form Components

You can work with form components similarly as to how you would work with a form builder.

Form Builders{.link}

Form Components{.link}

Configuring Forms

You can configure forms inline to an extent. Which is sometimes easier than writing a dedicated form builder.

Generally you can interact with the form criteria the same as you would with a form builder instance.

Form Builders{.link}

Form Configuration{.link}

Setting Options

You can set options by using the setOption method.

Form Options{.link}

{{ form('employment_application')
    .setOption('redirect', url_previous())
    .render()|raw }}