We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 630
    • 39 Posts
    Hello,

    In my feedback form I want to offer the possibility to determine the topic which can be either the particular document where the feedback link was activated, or the website as a whole. I tried to implement that but gave up believing that there must be something wrong with the validation mechanism of eForm. But much more likely the error is on my side.

    Approach I:
    Using a placeholder to be filled by the eFormOnBeforeFormMerge callback function

    <div class="form-radio-input" style="[+topicNoDisplay+]">
    <label for="topic">Concerning</div>
    <div id="topic"><input type="radio" name="topic" value="[+ref+]" checked="checked" eform="Topic::1:"/> the page »[+reftitle+]«<br />
       <input type="radio" name="topic" value="-1" /> <em>{{SiteName}}</em> in general</p>
    </div></div>
    


    That is, it appears to work when you use an event, e.g.:

    function onBeforeFormMerge (&$fields) {
         global $modx;
         $ref = $_REQUEST['ref'];
         if ($ref) {
             $pi = $modx->getPageInfo($ref);
             $fields['ref'] = $ref;
             $fields['reftitle'] = $pi['pagetitle'];
         }
         else $fields['topicNoDisplay'] = 'display:none;';
         return true;
    }
    


    The placeholders [+ref+] and [+reftitle+] appear properly populated then in the resulting html code. But filling out the form, the validation fails at the topic field. The debug infos of eForm reveal the value from [+ref+] as forgotten again:

    'Topic' Failed using rule: #LIST ,-1, (input=32)
    'Publication okay?' passed using rule: #LIST 1, (input=1)
    'formid' passed using rule: #VALUE feedback, (input=feedback)


    Approach II:
    It gets even weirder ...

    I learned just that snippet calls can be integrated in the eform chunk only with cached notation ([[...]]; [!...!] is left unparsed). So I created a snippet Get with the content
    <?php return $_REQUEST[$param]; ?>
    and called it in the chunk:
    ...<input type="radio" name="topic" value="[[Get? &param=`ref`]]" ...


    This way I got rid of the placeholder value which seems to interfer with the validation. The snippet call is interpreted as expected.

    What says the validation?
    'Topic' Failed using rule: #LIST 32,-1, (input=32)
    'Publication okay?' passed using rule: #LIST 1, (input=1)
    'formid' passed using rule: #VALUE feedback, (input=feedback)
    

    Seems like it refuses the input value although it has been included in the #LIST. So why, where is the problem?

    Your help is greatly appreciated,
    thank you,

    -- agilero.
      • 30223
      • 1,010 Posts
      The problem you are facing is the timing of the eFormOnBeforeFormMerge event. What happens is this:

      On each pass eForm parses the form and retrieves fields and values from the form template. It then calls the eFormOnBeforeFormMerge event, merges the placeholders and sends the page to the browser.

      Values for Select fields, radio and check boxes are retrieved when the form is parsed. These are the values used for validation. eForm does not know about the value you placed in the radio box as this is added to the radio box AFTER the form is parsed.

      What you can do instead is to use the eFormOnBeforeFormParse event instead. You probably only need to make a minor adjustment to the function:

      //<?php
      function onBeforeFormParse (&$fields, &templates) {
           global $modx;
           $ref = $_REQUEST['ref'];
           if ($ref) {
               $pi = $modx->getPageInfo($ref);
               $fields['ref'] = $ref;
               $fields['reftitle'] = $pi['pagetitle'];
           }
           else $fields['topicNoDisplay'] = 'display:none;';
           return true;
      }


      Then use &eformOnBeforeFormParse=`onBeforeFormParse` in your snippet call.
        • 630
        • 39 Posts
        I made the changes you are suggesting.

        First, I did it with value="[+ref+]" (Approach 1). This yielded the same error as I’ve had with onBeforeFormMerge, however.

        Then, approach 2 with value="[[Get? &param=`ref`]]". Indeed, that worked ... two or three times at least. But just when I turned to solving other outstanding problems in my contactform logic, suddenly the same indications came up again. Tried to restore the version that has worked, but nothing doing, not even emptying the cache. sad

        So I abandon this switch, above all merely nice-to-have from the beginning, as a simple hidden field will surely do as well, one which is filled just if the contact form is activated by clicking the bottom link "comment this page". The general contact link leads to the same contact form but with the ref field left empty.

        My eForm call:
        [!eForm? &debug=`1` &formid=`feedback` &tpl=`feedbackForm` &report=`feedbackMail` &sendAsText=`1` &subject=`[+short+]` &replyto=`[+address+]` !]


        The relevant line from {{feedbackForm}}:
        <input type="hidden" name="ref" value="[[Get? &param=`ref`]]" eform="commented page ID::1:"/>


        And here we are:
        'commented page ID' Failed using rule: #VALUE 5, (input=5)


        Is that your kind of humour? wink What do I wrong? I would also like to suggest you to revise your debugging info code. Contradictory messages like that don’t help anybody I’m afraid.

        -- agilero.