Dynamic ID of Perch Form to collect responses in different batches

  • I'm struggling to get the id of a form to be unique based on the page it is used on. The template renders but when the form is submitted I get a fatal error.

    The ID needs to be different as the responses need to be saved in separate lists based on the page.

    The opening tag to the form looks like:

    1. <perch:form id="<perch:forms id="formID" />" method="post" app="mbk_forms">

    Note: the error occurs whether or not its running through perch_forms or mbk_forms.

    The form renders OK on the page:

    1.  <form id="form1_tester" action="/landing-page-3" method="post">

    However when submitted I get:

    1. Fatal error: Uncaught Error: Call to a member function label() on null in /path/to-site/perch/addons/apps/perch_forms/runtime.php:27 Stack trace: #0 /path/to-site/perch/core/lib/Perch.class.php(134): perch_forms_form_handler(Object(PerchAPI_SubmittedForm)) #1 /path/to-site/perch/core/inc/forms.php(9): Perch->dispatch_form('tester:perch_fo...', Array, Array) #2 /path/to-site/perch/core/inc/forms.php(23): perch_find_posted_forms() #3 /path/to-site/perch/core/lib/Perch.class.php(235): {closure}(Object(PerchSystemEvent)) #4 /path/to-site/perch/core/runway/start.php(36): Perch->event('page.loaded') #5 /Users/tonyastley/Documents/Websites/SpeechWrite/2021-website/site- in /path/to-site/perch/addons/apps/perch_forms/runtime.php on line 27

    Can anyone help or suggest a different method to dynamically separate form responses?

  • Unfortunately your idea won't work.

    When perch gets the form attributes it fetches the content of the template file, so no template processing occurs. So when perch tries to get the ID it sees

    <perch:form id="<perch:forms id="formID" />" method="post" app="mbk_forms">

    instead of

    <form id="form1_tester" action="/landing-page-3" method="post">.

    This format also happens to break the regex used to find the ID of the form which is why you get the error

  • I don't think there is a way without creating a custom app or editing the perch_forms runtime file.

    For the most part your idea works, but the form tries to find the attributes to assign a form title. In the perch_forms/runtime file this is the section that is causing the issues (lines 26 - 31)

    1. $attrs = $SubmittedForm->get_form_attributes();
    2. if ($attrs->label()) {
    3. $data['formTitle'] = $attrs->label();
    4. }else{
    5. $data['formTitle'] = PerchUtil::filename($formKey, false);
    6. }

    As $attrs is being returned as null, I think if you just add a null check onto $attrs it should work