We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 32645
    • 377 Posts
    How do you get a return value from eFormOnValidate, and generate a [+validationmessage+] too?

    This is my code so far;

    [[OnBeforeMailSent]]
    [!eForm? &formid=`NLForm` &to=`[email protected]` &from=`[email protected]` &subject=`Newsletter Sign-up` &tpl=`Newsletter_Chunk` &report=`customreport` &eFormOnValidate=`mytestfunction`!]
    


    // OnBeforeMailSent snippet;
    function mytestfunction() 
    {
       // I want to add an error message to the Eform [+validationmessage+] at this point
       // I want to tell Eform to cease processing if the following return is `false`
       $mymessage = "This would be my validation message";
       return false;
    } // end function
    


    What I want to do is:


    1) Prevent EForm submitting, or sending the form.
    2) Add a custom message to [+validation message+]
    3) Make Eform recognise that a `false` return means stop processing the form and treat it like an error message.
    4) Get a return value from eFormOnValidate

    How do I do this?  Thanks.


    Is there any help/info on how best to use EformOnValidate, including how to add error messages, etc?

    Thanks
      • 32645
      • 377 Posts
      I’ve figured it out now.

      According to the documentation:


      &eFormOnValidate (optional)
      Expects a function name. This function will be called directly after the form validation has taken place. You can use this event to add your own extra validation logic. The function should accomodate the following parameters
      &$fields - (as reference) an associative array of field and placeholder values.
      &$vMsg - (as reference) a numeric array of validation error messages.
      &$rMsg - (as Reference) a numeric array of missing required fields.

      Which means that you can add to the &$vMsg array.

      Lets assume your form has 2 fields, 1 is name, the other is email. Your new message goes in array [3].

      Like this;

      function mytestfunction(&$fields, &$vMsg, &$rMsg) {
        global $modx;
      
      /*
      &$fields - (as reference) an associative array of field and placeholder values.
      &$vMsg - (as reference) a numeric array of validation error messages.
      &$rMsg - (as Reference) a numeric array of missing required fields.
      */
      
      $vMsg[3] = "Error Message";
      
      return $vMsg;
      } // end function
      


      In my test, this prevents the form from being submitted/committed and returns an error message, thus treating the return as a false and terminating the submission of the form.

      I’ve now solved my original issue and can work from there.

      Thanks.

      *Thread closed*
        • 32645
        • 377 Posts
        Does anyone know how to add a PHP created variable to your outgoing email digest report?

        ie:

        1. Eform call creates a unique activation key upon &eFormOnValidate
        2. Put activation key into a placeholder?
        3. Email digest report should allow usage of placeholder

        Problem I’m finding is that sometimes the placeholder is never there.

        So, do you need to add an entry to the &$fields parameter? If so, how do you put that into your email digest report?
          • 21561
          • 16 Posts
          Your post has solved my problem. Thanks grin

          If you still need help with your problem check out this post

          http://modxcms.com/forums/index.php/topic,27209.0.html
            • 32645
            • 377 Posts
            How do you make eFormOnValidate work with a GET command?

            <form method="get" action="[~[*id*]~]">
            </form>
            


            I have a form which will act as a search, but if I try to use GET eFormOnValidate never runs, however I can use GET variables inside eFormOnBeforeFormParse.
              • 30223
              • 1,010 Posts
              Quote from: worchyld at Feb 25, 2009, 11:02 AM

              How do you make eFormOnValidate work with a GET command?
              <form method="get" action="[~[*id*]~]">
              </form>
              

              I have a form which will act as a search, but if I try to use GET eFormOnValidate never runs, however I can use GET variables inside eFormOnBeforeFormParse.

              That’s correct. eFormOnBeforeFormParse is triggered every time eForm is called. eFormOnValidate on the other hand is only called when a form is posted and eForm only works with method="POST". When you use method="GET" eform never validates the form and will undertake no actions other than displaying the form. Unless you can use your search with POST you will not be able to use eForm for this.
                • 32645
                • 377 Posts
                TobyL

                I want to use Eform as a way to handle form processing and then perform a search.

                I do not want to use it for sending mail.

                Here’s what I’ve done;

                1) Built the form
                2) Created a custom library to handle all the validation outside of Eform
                3) Added `eFormOnBeforeFormParse` and `eFormOnBeforeMailSent` to the eform.
                4) Created a snippet with the relevant functions;

                [!eventFunctionSnippet!]
                [!eForm? &eFormOnBeforeFormParse=`formParseEvent`  &eFormOnBeforeMailSent=`formParseResults` &formid=`HSF` &tpl=`simple_form` &noemail=`0` &debug=`1`!]
                


                <?php
                //search-results
                function formParseResults( &$fields, &$templates )
                {
                // I want this to actually do the query that appears in formParseEvent
                }
                
                function formParseEvent( &$fields, &$templates, $debug=false )
                {
                	global $modx, $vMsg;
                        $debug = false;
                	if ($debug == true)
                        {
                	print '<pre>GET: '. print_r($_GET) . '</pre>';
                	print '<pre>POST:'. print_r($_POST) . '</pre>';
                	print '<pre>FIELDS: '. print_r($fields) . '</pre>';
                	print '<pre>vMsg: '. print_r($vMsg) . '</pre>';
                	print '<pre>rMsg: '. print_r($rMsg) . '</pre>';
                        print '<p>' . $modx->getPlaceholder('validationmessage') . '</p>';
                        }
                
                
                        global $modx, $vMsg;
                	
                        $output = '';
                	$basePath  = $modx->config['base_path'];
                	$siteURL	   = $modx->config['site_url'];
                
                	include_once( $basePath .'assets/snippets/curl/holiday.php' );
                
                	$Holiday = new Holiday(&$modx);
                        $sarray = $Holiday->processForm();
                        $outcome = $Holiday->build_sql($sarray);
                
                     // I cannot seem to use this placeholder anywhere, but in this function
                    // even if I return the placeholder
                     $modx->setPlaceholder('holidaySearchResults', $outcome);
                 
                return (true);
                
                } // end function
                ?>
                


                Here’s my problem.

                How do I get Eform to display the results as generated by the placeholder "holidaySearchResults"?

                How do I get the Eform to run the formParseResults action to perform the query?

                How do I make holidaySearchResults a global placeholder?

                I may end up just programming my own solution, because its really annoying me.
                  • 30223
                  • 1,010 Posts
                  Here’s what I’ve done;

                  1) Built the form
                  2) Created a custom library to handle all the validation outside of Eform
                  3) Added `eFormOnBeforeFormParse` and `eFormOnBeforeMailSent` to the eform.
                  4) Created a snippet with the relevant functions;

                  [!eventFunctionSnippet!]
                  [!eForm? &eFormOnBeforeFormParse=`formParseEvent`  &eFormOnBeforeMailSent=`formParseResults` &formid=`HSF` &tpl=`simple_form` &noemail=`0` &debug=`1`!]

                  Firstly it seems to me that the eFormOnValidate would be a better event to use because it only triggers when the form is submitted and secondly at least you’ll know that according to eForm everything validates. The fact that you validate the form results in your custom library does not turn off eForms validation.

                  How do I get Eform to display the results as generated by the placeholder "holidaySearchResults"?


                  Using modx->setPlaceholder stores the results for further processing by the modx document parser. This happens after eForm has finished and so the value will not be available to eForm ( unless you retrieve it yourself in code with $modx->getPlaceholder() ). You could simply add it to the $fields array ($fields[’holidaySearchResults’] = $outcome;) and later on retrieve it from there. At least I’m assuming that your aim here is to propagate the $outcome value to the next event function?


                  Come to think of it,... Why not place all your code in one event function. As far as I can see there’s no advantage in splitting this up into two ?
                    • 32645
                    • 377 Posts
                    Hi Toby,

                    Thanks for your replies.

                    I tried it in one function -- but I can never print out the output.

                    In the end I gave up and just custom built my own solution where the following happens.

                    1. Page loads
                    2. Run snippet that calls my custom library
                    3. Library grabs the post/get variables and does all the processing, and sets modx placeholders for [+messages+] and for [+searchOutput+] which includes a custom built clone of the loopDB pagination snippet.
                    3a) Grab the chunk templates I want to use and parse the chunks against DB row data (ie: http://wiki.modxcms.com/index.php/DB_query_and_Placeholders)
                    4. Display the page, complete with the placeholders created in 3.

                    I did this and it works fine. It even paginates fine.

                    I’ve now abandoned my requirement for using EForm to handle my problem.

                    Thanks.