We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 5463
    • 33 Posts
    any idea how can I integrate eForm with my custom spam validation? I’m new to ModX..if you can explain step by step would be much appreciatted.

    let’s say the normal PHP file is:

    <?php
    $rand = rand(3,9);
    $rand2 = rand(1,7);
    $sum = $rand+$rand2;
    
    if (isset($_POST['submit'])) {
    $summed = $_POST['sum'];
    
    $compare = substr($_POST['sumval'],4,-2);
    
    $comp = $compare/9;
    
    if ($summed== $comp) {
    return true; //somehow to continue with eForm email..etc
    
    }
    
    else { 
    return 'some error'; //somehow to stop with eForm and output the error
    
     }	
     }
    
    
    ?>
    <form name="captcha" method="post">
    <div><strong>
          <?=$rand?>
          </strong>+<strong>
            <?=$rand2?>
          </strong>=</div><br />
    
    <input type="text" class="textfield" name="sum" id="spam" <?=$borderspam?> /><?=$errspam?>
    <br />
    <input type="submit" name="submit" id="button" class="submitb" value="Submit" />
    <br />
    <input type="hidden" name="sumval" id="sumval" value="<?=$sum*9?>09">
          
          
          </form>


    how can I use it with eform form /callsnippet..etc

    thanks
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      Use one of eForm’s event traps and write a snippet with a function containing your code. Here’s the snippet I use to check on a "hidden" form field
      function checkField(&$fields,&$vMsg,&$rMsg) {
          if(!empty($fields['Last__Name'])) {
              return false;
          } else {
          return true;
          }
      }

      I added &eformOnValidate=`checkField` to my eForm snippet call. The snippet with your custom function has to be called before the eForm call, in order to make the function available to the eForm code when it runs.

      See the eForm documentation in the docs directory that comes with eForm for more on how to write event plugins for eForm.
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 5463
        • 33 Posts
        Thanks ,
        I found few functions like this, but I’m trying to understand how to put the random numbers on the form and then check against POST[hidden field] with the function..
        If you can translate my initial script into a modX format: like form is like this and check against POST[sum] etc..would be much appreciated.
        Not php expert, and don’t understand much modX that’s why I’m asking for step by step..

        thanks
          • 30223
          • 1,010 Posts
          //<?php
          //create "eformFunctions" snippet with this code
          
          function beforeFormMerge( &$fields ){
          	$rand = rand(3,9);
          	$rand2 = rand(1,7);
          	$sumval = (($rand+$rand2)*9). '09';
          
          	$fields['rand1'] = $rand;
          	$fields['rand2'] = $rand2;
          	$fields['sumval'] = $sumval;
          	
          	return true;
          }
          
          function formValidate(&$fields, &$vMsg, &$rMsg){
          	
          	$comp = substr($fields['sumval'],4,-2)/9;
          	
          	if( $fields['sum'] != $comp ) 
          	$vMsg['sum'] = "Anti spam solution is incorrect!";
          	
          	return true; 
          
          }
          
          return '';
          //?>
          


          Adapt your form template.
          <div><strong>
                [+rand1+]
                </strong>+<strong>
                [+rand2+]
                </strong>=</div><br />
          
          <input type="text" class="textfield" name="sum" id="spam" />
          <br />
          <input type="submit" name="submit" id="button" class="submitb" value="Submit" />
          <br />
          <input type="hidden" name="sumval" id="sumval" value="[+sumval+]"> /
          


          Call eForm and the formFunctions snippets in your document.

          [[eformFunctions]]
          [!eForm? &eFormOnBeforeFormMerge=`beforeFormMerge` &eFormOnValidate=`formValidate` ..(more params).. !]


          P.S. I didn’t check your original code so make sure the maths work smiley
            • 10449
            • 956 Posts
            hmm. I’ve now made a custom validation function, which seems to work (somewhat); i.e. using a legal input = form is sent. Incorrect value = blank screen.

            Is this "expected behaviour"? What do I need to do in order to display a custom error msg, or simply just display the form again with the erroneous input field marked via CSS? Just a blank page is not ideal :|
              • 30223
              • 1,010 Posts
              Show us what you’ve done... i.e. formTemplate, snippet call(s) and function(s).
              I find it rather difficult to assist without this.
                • 40045
                • 534 Posts
                @Susan

                Your idea with the hidden field sounds great, I don’t like captchas very much and so I’m looking for a better solution, could be that I found one, thanks to your post above...

                I just have one question...how has this hidden field to look like? Must it be hidden or must it be text like:

                <div class="input hidden">
                <input type="text" name="diespammer" />
                </div>

                so the css-class "hidden" makes the input display: none, so it isn’t visible for the normal user but a bot would fill it out?? Can a type="hidden" field be filled out?? I also don’t want to give the input an ID because I have sometimes more than one form on a page, is this a problem? I thing the input names are used, but I’m not sure...

                So I would do it like you proposed:

                Snipppet [[eForm_dieSpammer]]:

                function dieSpammer(&$fields,&$vMsg,&$rMsg) {
                if(!empty($fields[’diespammer’])) {
                return false;
                } else {
                return true;
                }
                }

                Call on in the modX document:
                [[eForm_dieSpammer]]
                [[eForm? ... &eFormOnValidate=`dieSpammer` ... ]]

                (have to make the snippet calls uncached because I use Ditte all over the place =D)...

                thanks for your reply!
                  • 36805
                  • 354 Posts
                  Quote from: ganeshXL at Aug 15, 2008, 01:44 AM

                  Is this "expected behaviour"? What do I need to do in order to display a custom error msg, or simply just display the form again with the erroneous input field marked via CSS? Just a blank page is not ideal :|
                  In case you (or someone else) still want(s) to know : While return true works fine, you *must not* return false in your eFormOnValidate function. Use a plain return instead and your form and its messages will show.