Search code examples
fieldnamingtaggingwebhooksformstack

Is it possible to name (or tag) FormStack fields with simple identifiers?


This question assumes familiarity with FormStack, a drag-and-drop WYSIWYG online form builder.

Background

A client is using FormStack to manage forms. Currently, form submissions are emailed, then manually entered into a database. Predictably, my task is to automate this process. This is easy enough using FormStack's WebHooks API: I can have form submissions sent to a URL, e.g. a PHP script, and happily parse away.

Question

Is it possible to name (or tag) FormStack fields with simple identifiers?

The client needs to be able to customize the form such that multiple fields may feed into the same database column.* FormStack however, as far as I can tell, provides only a way to specify a field label, e.g. Which of these trips interest you?, not a programmer-friendly identifier, e.g. Trip. My script would have to string-compare labels (which, due to their length, are more prone to typos) to determine what to do. What are some sensible workarounds to this problem?

Clarifications*

The reason there can exist multiple fields that feed into the same database column, is that the client uses conditional fields. For example, one field might ask, Where are you studying abroad? If the user selects "Europe", a conditional field might appear, asking Which of these trips interest you?, with choices pertaining to Europe. If the user selects "Africa" however, a similar field might appear, e.g. Which of these trips interest you?, but with choices pertaining to Africa. In FormStack, these are actually two distinct fields. However, as you can imagine, the values belong in the same database column, Trip.


Solution

  • I have settled on a hack for now. FormStack allows HTML markup in labels, e.g. Which of these trips interest you? <!--Trip-->. The client is willing to "tag" fields in this way.

    Here's a snippet of the code that parses such tags, in case it might help someone else:

        require_once 'Formstack.php';
        $formstack = new Formstack($apiKey);
        $form = $formstack->form($_POST['FormID']);
        $taggedFields = array();
        foreach ($form['fields'] as $field)
        {
            if (preg_match('/<!--\s*([0-9A-Za-z]+)\s*-->/',
                           $field['label'],
                           $matches))
            {
                $taggedFields[$matches[1]] = $_POST[$field['id']];
            }
        }
    

    In fact, I've had to make it a little bit more sophisticated. Some FormStack field-types serialize input (in a horrific way). For example, FormStack's Name field-type takes multiple fields (prefix, first, middle, last, initial, suffix), and concatenates the results into a string:

    'first = Andrew
    initial = W
    last = Cheong'
    

    To handle this, I've written my code to handle such syntax in labels as Tell us your name! <!--FirstName=first--> <!--LastName=last--> <!--MiddleInitial=initial-->

    The code follows.

        require_once 'Formstack.php';
        $formstack = new Formstack($apiKey);
        $form = $formstack->form($_POST['FormID']);
        $taggedFields = array();
        foreach ($form['fields'] as $field)
        {
            if (preg_match_all('/<!--\s*([0-9A-Za-z]+)\s*(?:=\s*(\w+))?-->/',
                               $field['label'],
                               $matches,
                               PREG_SET_ORDER))
            {
                foreach ($matches as $captures)
                {
                    if (count($captures) == 3 &&
                        preg_match('/(?:^|\n|\r)'.$captures[2].' = ([^\n\r]+)/',
                                   $_POST[$field['id']],
                                   $subcaptures))
                    {
                        $taggedFields[$captures[1]] = $subcaptures[1];
                    }
                    else
                    {
                        $taggedFields[$captures[1]] = $_POST[$field['id']];
                    }
                }
            }
        }
    

    Hopefully, FormStack will soon add a native way to name or tag fields!