I know this is an old posting, but I wanted to leave this here in case anyone (like me) needs it in the future. {{ form.email }}
and {{ form.body }}
are the only tags that will function this way - for the email and body fields. If you need to get data for other custom fields, you need to use a bit of a hack.
When the form returns with an error, the data from the submitted fields is available in the query string of the page. There is a great article from FreakDesign that shows how to pull that into your templates.
I wound up having to customize it some, though, for use with the Shopify contact form specifically. Here is what worked for me:
{% comment %}
IF THE FORM THROWS ERRORS, POPULATE THE FORM FIELDS WITH DATA FROM THE QUERY STRING
NOTE: To use the `{% if form.errors %}` statement below, you must place this code within the `{% form 'contact' %}` tag.
{% endcomment %}
{% if form.errors %}
{%- comment -%}
Liquid by Jason @ freakdesign.
Questions? Ping me on twitter: @freakdesign
Relates to blog post:
http://freakdesign.com.au/blogs/news/get-the-url-querystring-values-with-liquid-in-shopify
Example:
https://jasons-experiments.myshopify.com/collections/all/products/3-4-sleeve-kimono-dress-coral-1?ref=freakdesign&cache=false
{%- endcomment -%}
{%- comment -%} Capture the content for header containing the tracking data {%- endcomment -%}
{%- capture contentForQuerystring -%}{{ content_for_header }}{%- endcapture -%}
{% comment %} Use string splitting to pull the value from content_for_header and apply some string clean up {% endcomment %}
{%- assign pageUrl = contentForQuerystring | split:'"pageurl":"' | last | split:'"' | first | split:'.myshopify.com' | last |
replace:'\/','/' |
replace:'%20',' ' |
replace:'\u0026','&' |
replace:'%5B','[' |
replace:'%5D',']' |
replace:'+',' '
-%}
{% assign debug = false %}
{%- for i in (1..1) -%}
{%- comment -%} If the string doesn't contain a ? then we have no querystring. Go no further {%- endcomment -%}
{%- unless pageUrl contains "?" -%}{% break %}{%- endunless -%}
{%- comment -%} Split the url at the ? to get all values after it {%- endcomment -%}
{%- assign pageQuerystring = pageUrl | split:'?' | last -%}
{%- comment -%} Split the remaining string at & to get the list of keys and values (if any) {%- endcomment -%}
{%- assign parts = pageQuerystring | split:'&' -%}
{% assign formFirstName = '' %}
{% assign formLastName = '' %}
{% assign formPhone = '' %}
{% assign formMethod = '' %}
{% assign formReason = '' %}
{%- comment -%} Loop over them... {%- endcomment -%}
{%- for part in parts -%}
{%- comment -%} Split the part at the =. Not all querystrings will be in pairs so we need to account for that {%- endcomment -%}
{%- assign keyAndValue = part | split:'=' -%}
{%- if keyAndValue.size > 1 -%}
{%- if debug -%}
<!--
key: {{ keyAndValue[0] }}<br>
value: {{ keyAndValue[1] }}
-->
{% endif %}
{% case keyAndValue[0] %}
{% when 'contact[First Name]' %}
{% assign formFirstName = keyAndValue[1] %}
{% when 'contact[Last Name]' %}
{% assign formLastName = keyAndValue[1] %}
{% when 'contact[Phone Number]' %}
{% assign formPhone = keyAndValue[1] %}
{% when 'contact[Preferred Method Of Communication]' %}
{% assign formMethod = keyAndValue[1] %}
{% when 'contact[Reason For Inquiry]' %}
{% assign formReason = keyAndValue[1] %}
{% endcase %}
{%- else -%}
{%- if debug -%}
<!--
value: {{ keyAndValue }}
-->
{%- endif -%}
{%- endif -%}
{%- endfor -%}
{%- endfor -%}
{% endif %}
Works like a charm. Hope that helps!