We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 27376
    • 576 Posts
    Is it possible to populate eform with data on runtime using one of the events provided?

    I tried populating the $_POST variable with data but that threw eform in to validation mode. I want to be able to have a chunk containing a form with inputs that are empty, but when I run it through eForm, the input fields will be populated with data.

    Any suggestions as to how I can acheive this?

    UPDATE See next 2 posts for solutions.
      • 30223
      • 1,010 Posts
      Yes you can...

      Firstly you need to put some place holders in your form template. Using the eFormOnBeforeFormParse event you can then replace the placeholders with actual values. Where these values come from is up to you...

      example:
      <!-- form stuff -->
      <input type="text" name="mySubject" value="[+mySubject+]" />
      <textarea name="myComments">[+myComments+]</textarea>
      <!-- more form stuff -->
      


      The event function:
      <?php
      function beforeParseFunction(&$fields, &$templates){
          //some arbitrary values
          $values = array(
              "myComments" => "Here's some comments\n on two lines",
              "mySubject" => "This is the subject"
          };
          
          $templates['tpl'] = formMerge($templates['tpl],$values);
          
          return true;
      }
      ?>
      


      See the eForm documentation (in assets/eform/docs/) on how to insert this function and call events.
        • 27376
        • 576 Posts
        I actually stumbled across this by accident, but I found that it’s not even necessary to run formMerge with the method above, try this:
        <?php
        function beforeParseFunction(&$fields, &$templates) {
            if (empty($_POST)) {
                $fields['myComments'] = "Here's some comments\n on two lines";
                $fields['mySubject'] = "This is the subject";
            }
            return true;
        }
        ?>

        The function first checks for empty POST data, if it IS empty, then it will populate $fields with some data whose keys correspond to placeholder in my form. The values will be placed in the form on the next formMerge call, which happens just before the form is outputted to the browser!

        Thanks TobyL for this amazing script, you’ve saved me a load of time with this script. The form validation is by far the shining feature of this snippet!
        • We have a survey consisting of 20 questions, with approximately 5 RADIOs per question. However, each question is defined in a database and tied back to a survey_id, allowing for multiple surveys, with varying questions, etc...

          OK ok...that aside, what if I wanted to use eForm for the form validation purposes, but my fields themselves are dynamically created based on DB definitions

          e.g. output
          <input type="radio" name="Answer1" value="5">Always
          <input type="radio" name="Answer1" value="4">Usually
          <input type="radio" name="Answer1" value="3">Sometimes
          <input type="radio" name="Answer1" value="2">Seldom
          <input type="radio" name="Answer1" value="1">Never
          
          <input type="radio" name="Answer2" value="1">True
          <input type="radio" name="Answer2" value="0">False
          etc...
          


          Is there a way I can pass my dynamically created form (from a Snippet) for use within eForm (i.e. &tpl=`dynamicForm_fromSnippet` ) huh
            Mike Reid - www.pixelchutes.com
            MODx Ambassador / Contributor
            [Module] MultiMedia Manager / [Module] SiteSearch / [Snippet] DocPassword / [Plugin] EditArea / We support FoxyCart
            ________________________________
            Where every pixel matters.
            • 30223
            • 1,010 Posts
            I’ve been considering extending eForm so you can set template parameters directly to a snippet. I may have a look at that again. In the mean time it is entirely possible doing this using the eformOnBeforeFormParse event.

            It should be possible to create a survey snippet which will create all the different templates (form, report etcetera) dynamically. You will however still need to supply some default chunks for the various templates as otherwise eForm will spit the dummy before it reaches the event. Your database would off course have to supply the validation rules and if a field is required as well..

            You could use eForm’s other events to store the results in a database as well.

            I’ve actually done something like this in the past, before modx and without eForm, using simple ini files for conference registrations. If you need help... I’m open for some paid work grin

            • Quote from: pixelchutes at Jan 08, 2007, 09:18 PM

              Is there a way I can pass my dynamically created form (from a Snippet) for use within eForm (i.e. &tpl=`dynamicForm_fromSnippet` ) huh

              Want snippet-returned HTML for use in eForm? Here’s one way in 5 easy steps (line references from eForm v1.4.3)

              I just implemented a solution for using Snippet-returned data as &tpl=`...` for eForm!

              No more than a few lines required modification...still should be considered BETA, but you might want to check this out, because it’s working like a charm! wink

              Here’s the snippet TPL implementation requirements:

              replace lines 68-69 of:
              	
              	//required
              	if( $tmp=efLoadTemplate($tpl) ) $tpl=$tmp; else return $_lang['ef_no_doc'] . " '$tpl'";
              

              with:
              	
              	// Modification by pixelchutes Allows for snippet-returned dynamic forms (3:17 PM 1/8/2007)
              	if( empty( $tpl ) and !empty( $tplSnippet ) ) $tpl = $tplSnippet;
              
              	//required 	# Modification by pixelchutes (pass optional tplSnippet)
              	if( $tmp=efLoadTemplate($tpl,$tplSnippet) ) $tpl=$tmp; else return $_lang['ef_no_doc'] . " '$tpl'";
              

              Since other line references below depend on it, ensure that line 71 reads:
              	
              	//required 	# Modification by pixelchutes (pass optional tplSnippet)
              



              Let’s continue. Now, update new line 1014 to:
              	function efLoadTemplate($tpl,$tplSnippet=null){ // pixelchutes (tplSnippet param)
              

              replace lines 1025-1026 of:
              }else if($tpl)
              $tpl = ( $chunk=$modx->getChunk($tpl) )? $chunk : false;
              

              with this:
              }
              // Next 4 lines modified by pixelchutes to allow eForm templates from a snippet ( 2:59 PM 1/8/2007 )
              else if( $tpl and empty( $tplSnippet ) ) 
              	$tpl = ( $chunk=$modx->getChunk($tpl) )? $chunk : false;
              else if( $tpl == $tplSnippet ) 
              	$tpl = ( $snippet=$modx->runSnippet( $tplSnippet ) )? $snippet : false;
              


              Then, update the eForm snippet itself to check for &tplSnippet=`...`, by adding the following to the $params array
              NOTE: I added mine on line 59, right after tpl => isset($tpl)? $tpl:"",
              	
              	tplSnippet => isset($tplSnippet)? $tplSnippet:"", // Added by pixelchutes
              



              Finally, in the snippet call, don’t pass &tpl, rather &tplSnippet=`MySnippet_returnsHtmlForm`

              e.g.
              [!eForm? &formid=`myform` &tplSnippet=`MySnippet_returnsHtmlForm` !]
              


              What’s so great is that I was able to port our Assessment/Survey system directly into eForm! With dynamic questions, and answer options/types (numeric, string/checkbox, radios, etc) a few simple rules to build out the form with eform="..." on the <input>, and now we have validation options, email capabilities, as well as post-processing through event functions right out of the box wink

              NOTE: This is just a personal, temporary solution. The primary fall back with this approach is that other templates (thankyou, report, etc) aren’t included. I’ve sent TobyL the idea, and he said he’ll be looking into a more generic solution specifically so that you can apply this to any of the templates
                Mike Reid - www.pixelchutes.com
                MODx Ambassador / Contributor
                [Module] MultiMedia Manager / [Module] SiteSearch / [Snippet] DocPassword / [Plugin] EditArea / We support FoxyCart
                ________________________________
                Where every pixel matters.
                • 24292
                • 28 Posts
                I’ve just been trying eformOnBeforeFormParse to populate a <select> field.

                First the documentation I have for V1.4.1 says
                The function should accomodate the following parameter: &$templates
                This should be &$fields, &$templates.

                Once I got that worked out I tried
                $fields['people'] = $val;
                but it always reported the field invalid when the form was submitted.

                Next I tried
                $templates['tpl'] = formMerge($templates['tpl'], array ('people' => $val));

                That worked but never reported any errors even when required fields were left empty.

                Finally I tried
                $templates['tpl'] = str_replace('[+people+]', $val, $templates['tpl']); 
                and that seems to works perfectly.

                Does anyone know if this is correct way to do this?
                  • 27376
                  • 576 Posts
                  The formMerge function removes all the eform="" tags from your chunk, that is why there is no validation when you submit the form. What you need to do is check to see if the form is submitted.
                  I’ve created a small tutorial on how to accomplish what you are trying to do on the wiki: HOWTO: Populate eForm with dynamic data.
                  • Nice, now we just need to update the Wiki to show how to populate <select> options from a DB, into eForm wink
                      Mike Reid - www.pixelchutes.com
                      MODx Ambassador / Contributor
                      [Module] MultiMedia Manager / [Module] SiteSearch / [Snippet] DocPassword / [Plugin] EditArea / We support FoxyCart
                      ________________________________
                      Where every pixel matters.
                      • 27376
                      • 576 Posts
                      Quote from: pixelchutes at Feb 27, 2007, 03:39 AM

                      Nice, now we just need to update the Wiki to show how to populate <select> options from a DB, into eForm wink
                      I’m still struggling with that one! :-/

                      It’s a dynamically generated <select> list too...