Skip to main content

Custom Form Validation

This guide shows how to integrate common client-side form validation frameworks such as Bootstrap, Parsley.js and Foundation Abide.

Parsley.js

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.

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...

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

1

Inside the form builder for the applicable form, go to the Settings tab.

At the bottom of the General section in the Settings tab, there's an area called Attributes. In the Form tab, 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

To add Parsley attributes to fields, go to the Layout tab of the form builder, click on any field, and in the property editor on the left, you'll see an Attributes settings area. You will likely be appling these attributes to the Input tab.

For example:

  1. In the Input tab, add required to the Attribute column and leave the Value column empty (or specify true) to make the field required.
    • Merely toggling on the Require this field setting is not sufficient to trigger Parsley, as it's intended just for Freeform's own validation.
  2. 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("myForm").render() }}

Bootstrap

Bootstrap is the most widely used HTML framework. It includes a low level of form validation which you can trigger for your forms. This guide will show you how to implement form validation for your Freeform forms with Bootstrap validation.

This guide assumes that you have a Bootstrap 4 based website which includes Bootstrap CSS and JS files.

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

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

1

Inside the form builder for the applicable form, go to the Settings tab.

At the bottom of the General section in the Settings tab, there's an area called Attributes. In the Form tab, add novalidate in the Attribute column and leave the Value column empty (or specify true). This will disable HTML form validation in your web pages and allow Bootstrap to take over. Also add a form class like needs-validation so you can then trigger validation on your form by targeting this class as below.

AttributeValue
novalidate
classneeds-validation
2

Render your form builder-based form in a Twig template as usual.

{{ freeform.form("myForm").render() }}
3

At the bottom of your page, add javascript to attach validation to your form(s). Note the example class needs-validation. This needs to match the class name you set on your form inside the form builder in the steps above.

<script>
// Example starter Javascript for disabling form submissions if there are invalid fields
(function() {
'use strict';
window.addEventListener('load', function() {
// Fetch all the forms we want to apply custom Bootstrap validation styles to
var forms = document.getElementsByClassName('needs-validation');
// Loop over them and prevent submission
var validation = Array.prototype.filter.call(forms, function(form) {
form.addEventListener('submit', function(event) {
if (form.checkValidity() === false) {
event.preventDefault();
event.stopPropagation();
}
form.classList.add('was-validated');
}, false);
});
}, false);
})();
</script>

Foundation Abide

The Abide plugin for Zurb Foundation extends native HTML form validation. This guide will show you how to implement form validation for your Freeform forms with Abide.

This guide assumes you are running Foundation 6 and have the Abide plugin correctly installed on your web page.

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

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

1

Inside the form builder for the applicable form, go to the Settings tab.

At the bottom of the General section in the Settings tab, there's an area called Attributes. In the Form tab, add novalidate in the Attribute column and leave the Value column empty (or specify true). This will disable HTML form validation in your web pages and allow Foundation Abide to take over. Also add data-abide in the Attribute column and leave the Value column empty (or specify true). This triggers Abide level validation.

AttributeValue
novalidate
data-abide
2

To add Parsley attributes to fields, go to the Layout tab of the form builder, click on any field, and in the property editor on the left, you'll see an Attributes settings area. You will likely be appling these attributes to the Input tab.

For example:

  1. In the Input tab, add required to the Attribute column and leave the Value column empty (or specify true) to make the field required.
    • Merely toggling on the Require this field setting is not sufficient to trigger Abide, as it's intended just for Freeform's own validation.
  2. You can also add extra validations like number or email to trigger more complex predefined patterns as seen here. Note that some attributes may have blank values, and that's okay.
    AttributeValue
    required
    patternemail
3

Render your form builder-based form in a Twig template as usual.

{{ freeform.form("myForm").render() }}