Skip to main content

Form Validation with Parsley

Parsley is a very well documented and capable form validation Javascript library. This guide will show you how to implement form validation for your Freeform forms with Parsley.

Overview

This guide assumes you are running Parsley on your web pages. Usually this just means including the parsley.js or parsley.min.js file in your source code. Parsley relies on jQuery as well, so be sure that is included in your site as well before you follow the next steps.

You can implement Parsley validation for your form 1 of 2 ways...

Inside the Form Builder (CP)

You can add the necessary attributes to your forms with the form builder. Follow the instructions below to see how.

1
  • Open up an existing form inside Freeform and go to the Form Settings tab (cog/gear icon).
  • At the bottom of the settings tab, there's an area called Form tag Attributes. Add data-parsley-validate in the Attribute column and leave the Value column empty (or specify true). This will trigger the Parsley validation.
AttributeValue
data-parsley-validate
2
  • For the rest of your fields, add attributes to them in the form builder interace as needed. To add attributes to fields, simply click the field in the middle layout column, and in the right side Property Editor column, you'll see an Input attributes settings area. For example:
    • Add required to the Attribute column and leave the Value column empty (or specify true) to make the field required.
      • Merely checking the This field is required checkbox is not sufficient to trigger Parsley, as it's intended just for Freeform's own validation.
    • You can also add extra validations like data-parsley-maxlength="20" (data-parsley-maxlength in Attribute column and 20 in Value column) or data-parsley-type="number" to trigger more complex built-in validations as seen here.
      AttributeValue
      required
      data-parsley-typenumber
      data-parsley-maxlength20
3
  • Render your form builder-based form in a Twig template as usual.
    {{ freeform.form("parsleyForm").render() }}
Finished!

At Template Level

You can add the necessary attributes and changes to your forms directly inside your Twig templates with the Freeform form.renderTag(). Follow the instructions below to see how.

1
  • Add the data-parsley-validate attribute to your form using the renderTag() method like this:
    {% set form = freeform.form("parsleyForm", {
    formAttributes: { "data-parsley-validate": true }
    }) %}
    {{ form.renderTag({}) }}
    ...
    {{ form.renderClosingTag }}
2
  • To set field level attributes, you can apply them directly to your form field inputs or field.render. Usually it's more convenient to set these inside the Freeform control panel form builder area, but you can still control these at the template level if you want. Your template code might look something like this:
    {% set form = freeform.form("parsleyForm", {
    formAttributes: { "data-parsley-validate": true }
    }) %}
    {{ form.renderTag({}) }}
    {% for row in form %}
    <div>
    {% for field in row %}
    <div>
    {% set attr = {} %}
    {% if field.required %}
    {% set attr = attr|merge({'required':true}) %}
    {% endif %}
    {% if field.type == 'number' %}
    {% set attr = attr|merge({'data-parsley-type':'number'}) %}
    {% endif %}
    {{ field.render({
    inputAttributes: attr
    }) }}
    </div>
    {% endfor %}
    </div>
    {% endfor %}
    {{ form.renderClosingTag }}
Finished!