Form Tag HTML Objects

When you are working with forms in your form templates you will usually be working with a HTML Object. 

The purpose of the HTML Object is to allow you to add or change the attributes for the form tag before outputting the form tag. Instead of having to manually build out the form tag just to set a custom class value you can set the attribute value and then simply output the form tag. This helps with readability and is often easier to write.

Instead of doing this:

<form method="{{ form.method }}" action="{{ form.action }}" enctype="{{ form.enctype }}" class="form-class">

now you can set the class value and then simply output the code:

{% set form.class = 'form-class' %}
{{ form.openTag }}

Opening and closing form tag

You'll notice in the above code example that {{ form.openTag }} is used instead of {{ form.tag }}. Because the form tag is made up of two tags (an opening and closing tag) and you always customize the code between the opening and closing tags, you can access those tags through {{ form.openTag }} and {{ form.closeTag }}. Technically {{ form.tag }} will output the opening <form> tag, but it's more clear to use {{ form.openTag }} to represent the opening form tag.

Available form attributes

Typically the following form attributes are available for you to set or change values:

{{ form.action }}
{{ form.class }}
{{ form.enctype }}
{{ form.id }}
{{ form.method }}
{{ form.name }}
{{ form.style }}
{{ form.target }}
{{ form.title }}

'class', 'id', 'style', 'target' and 'title' may not always have values.

You can also set any valid HTML attribute on the form tag such as 'data' attributes.

See HTML Objects for more information about setting attribute values.