The form object

The form object is used within the form tag. It contains attributes of its parent form.

form.id

Returns the id (unique identifier) of the form.

form.author

Returns the name of the author of the blog article comment. Exclusive to form tags with the "article" parameter.

form.body

Returns the content of the blog article comment. Exclusive to form tags with the "article" parameter.

form.email

Returns the email of the blog article comment's author. Exclusive to form tags with the "article" parameter.

form.errors

Returns an array of strings if the form was not submitted successfully. The strings returned depend on which fields of the form were left empty or contained errors. Possible values are:

  • author
  • body
  • email
  • form

Input

{% for error in form.errors %}
    {{ error }}
{% endfor %}

Output

<!-- if the Name field was left empty by the user -->
author

You can apply the default_errors filter on form.errors to output default error messages without having to loop through the array.

Input

{% if form.errors %}
      {{ form.errors | default_errors }}
{% endif %}

Output

Please enter a valid email address.

If you want more control over the markup of the errors, you can loop through the messages and translated_fields arrays that are part of the form.errors object.

<ul>
    {% for field in form.errors %}
      {% if field == 'form' %}
        <li>
              {{ form.errors.messages[field] }}
      </li>
      {% else %}
      <li>
          {{ form.errors.translated_fields[field] }} - {{ form.errors.messages[field] }}
        </li>
    {% endif %}
    {% endfor %}
</ul>

form.set_as_default_checkbox

Renders an HTML checkbox that can submit the current form as the customer's default address. Exclusive to form tags with the "address" parameter.

Input

{{ form.set_as_default_checkbox }}

Output

<input type="checkbox" id="address_default_address_12345678" name="address[default]" value="1">

form.first_name

Returns the first name associated with the address. Exclusive to form tags with the "address" parameter.

form.last_name

Returns the last name associated with the address. Exclusive to form tags with the "address" parameter.

form.company

Returns the company name associated with the address, if it exists. Exclusive to form tags with the "address" parameter.

form.address1

Returns the first address line associated with the address. Exclusive to form tags with the "address" parameter.

form.address2

Returns the second address line associated with the address, if it exists. Exclusive to form tags with the "address" parameter.

form.city

Returns the city associated with the address. Exclusive to form tags with the "address" parameter.

form.province

Returns the province or state associated with the address. Exclusive to form tags with the "address" parameter.

Input

{{ form.city }}, {{ form.province }}

Output

San Francisco, California

form.country

Returns the country associated with the address. Exclusive to form tags with the "address" parameter.

form.zip

Returns the zip code or postal code associated with the address. Exclusive to form tags with the "address" parameter.

form.phone

Returns the telephone number associated with the address, if it exists. Exclusive to form tags with the "address" parameter.

form.posted_successfully?

Returns true if the form was submitted successfully, or false if the form contained errors. All forms but the address form set that property. The address form is always submitted successfully.

{% if form.posted_successfully? %}
    Comment posted successfully!
{% else %}
    {{ form.errors | default_errors }}
{% endif %}

Want to discuss this page?

Visit the Shopify Community