Wix Service Plugins (formerly SPIs and custom extensions) allow you to implement custom logic to change how your site behaves and displays using Velo SPIs. For example, when you set up a form from the Wix Form app, there are a small number of validations to choose from. The form submission service plugin allows you to implement your own submission validations and options using code. You can also connect your site to form validation providers not currently supported by Wix. Any violations to these validations can be displayed on the form.
You can manage service plugins from your site's dashboard, and they behave just like the ones Wix already supports. Learn more about using service plugins.
With the Form Submission service plugin you can define the submission validations for a form that fit your site's needs. Possible validations include:
This tutorial explains how to set up and implement a form submission plugin on your site using Velo. We demonstrate how to validate your form submission by checking whether a site visitor entered a name correctly in alphabetical characters only.
The process has 3 steps:
The first step in setting up your new plugin is to add it to your site. This process creates a new folder in the Service Plugins section of the Velo sidebar, which contains the files for your code.
The procedure in the previous step creates a folder in the service plugins section of the Velo sidebar. The name of the folder is based on the plugin you chose. Inside this is another folder with the name of the plugin you set up. This folder contains 2 files, your-plugin-config.js and your-plugin.js. In our case the files are named submission-config.js and submission.js. We implement the custom code for our plugin in these files.
This file is where we write the code for setting up the plugin's configuration.
The code in this file defines a function named getConfig()
that returns an object containing the values used to display the form submission validations on your site.
There are several apps that use forms. To validate form submission for a specific Wix app, we need to configure the file by defining the relevant namespace
field in the plugin. In our example, we want to validate a form in the Wix Forms app, so we set the namespace
value as wix.form_app.form
in the service plugin's configuration file and enable the validation.
This file is where we write the code for validating the first name form field. A site visitor tries to enter a first name with a non-alphabetical character. If there are any violations to the submission validation, a warning message can appear on the form as a text box.
The code in this file defines a function named validateSubmission()
. This function is called by Wix Forms to retrieve any violations to the validations provided by our plugin. The function accepts the following parameter:
options
: An object containing data about the source of the request, and the form submission information to validate. This information must include form ID. For more details, see the Service Plugin Reference.
The validateSubmission()
function returns an array of validation objects (any validation violations in a site visitor's form). These validation violations can be displayed in the site visitor's form as text boxes with warnings. If there are no validation violations, the response is an object containing an empty list.
Line 1: First we import the Wix Forms submission provider module.
Line 3-7: We need to define the form ID so that the code would know which form to validate. To get the form ID, call querySubmissionsByNamespace()
. We don't need to define the form field values separately in the submissions
object, as the values will be taken directly from the published site form.
Line 9: Then we export the validateSubmission()
function where we set the custom logic for our validations.
Lines 10: We save the first name field value into the firstNameValidation
variable.
Line 11: Define the Regex pattern so that the field only accepts alphabetical characters.
Line 12: Here we check if a first name field value matches the Regex pattern.
Lines 13-20: Set the violation description that can be displayed when there is a violation to the set pattern. If the form field value doesn't match the pattern, you get an error, and also form submission is aborted.
If you don't want to keep all of your code in the main files, you can add files to the plugin's folder and import functions and objects into the main files. To do this, create a new file in the plugin's folder. To import from these files to the main files, use the following syntax:
You can test your plugin before publishing your site using functional testing like you would with any backend Velo code. Make sure your validateSubmission()
function's return values are properly formatted.
You can test your plugin after deploying. To do this, add console logs to your code. The results appear in the Site Events log.
Once your code files are ready, publish your site. Navigate to your site, and fill in a form. Any violations to the validations you've customized are displayed on the form.
If a site visitor tries to enter a first name with non-alphabetical characters, there will be an error message that could be shown on the site.
The validation violation response looks like this:
You can remove the plugin from your site. Do the following: