We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 30319
    • 406 Posts
    I want to validate the answer for Formit's math hook/captcha...

    I want to validate that fi.value entered into the form really is the correct answer for

    fi.op1 operator fi.op2

    Formit validator would be [[!FormIt? &validate=`math:isCorrectAnswer`]]

    <air code>
    $answer = calculate fi.op1 operator fi.op2 taken from form
    if is true math = answer then validate
    else show validation error message
    end if
    </air code>

    I'm at a loss to know how to pass the values back and forth between the form and snippet...
    Has anyone done this before??

    I do know about the 'isBigEnough' validator at
    http://rtfm.modx.com/display/ADDON/FormIt.Validators

    Thank you, Tom

    This question has been answered by anso. See the first response.

      • 1778
      • 659 Posts
      Hi Tom
      Create a snippet call it as you want (let say 'mathValidator' ) and put this code in it
      $value = $value; // the input value in the field math
      
      $op1 = $_POST['op1'] ; // value of field op1 
      $op2 = $_POST['op1'] ; // value of field op1 
      $operator = $_POST['operator'] ; // value of field operator (should be + or - )
      
      switch($operator){
          case '+':
              $goodresult = $op1 + $op2; // result  if operator is 'plus' ...
              if($value!=$goodresult){ // if answer not equal expected result
                  $validator->addError('math', 'The answer is false.');
                  return false;
              }
              break;
           case '-':
              $goodresult = $op1 - $op2;
              if($value!=$goodresult){
                  $validator->addError('math', 'The answer is false.');
                  return false;
              }
              break; 
              
      }
      return true;
      


      It should work (i did not test it but...)
      in your formit call don't forget to add

      [[!FormIt?  
      &hooks=`email,redirect,math`  
      &emailTpl=`email`
      &emailSubject=`Website Inquiry` 
      &emailTo=`[email protected]`
      &emailCC=`[[+email]]`
      &emailFromName=`[[+name]]`
      &redirectTo=`27`
      &customValidators=`mathValidator`,  // this line
      &validate=`math:required:mathValidator` // and this one
      ]]
      
      


      and remove the ':required' in the <input...>... Formit now needs to have customValidators explicitly declared in the snippet call, and the validation calls are now done via property '&validate'.
      It should work with the latest versions of Formit (if yours is not the last I recommand to update)
      Let me know if you run in trouble...
      Cheers
        • 30319
        • 406 Posts
        I will test...code looks good, I'm not a programmer so I could not have done that as quickly as you did. Thank you for taking time out to do that, hope you solve your gallery issue.
        Thank you, Tom [ed. note: TomMLS last edited this post 12 years, 2 months ago.]
          • 1778
          • 659 Posts
          Hope it will work for you...
          My Gallery issue is not solved at this time.. I guess the problem is somewhere with phpThumb but can't find where and how to solve... Hope someone in the forum have some clues...
          Cheers
            • 30319
            • 406 Posts
            Hello -- finally tested -- this always gives error message even when the answer is correct!! sad
            Hope you have time to re-read the code, or p'raps someone else can read it...

            How can I have the snippet echo back the values, particularly $value and

            Thank you, Tom [ed. note: TomMLS last edited this post 12 years, 1 month ago.]
            • discuss.answer
              • 1778
              • 659 Posts
              Hi Tom

              Try ot change
              $op1 = $_POST['op1'] ;
              $op2 = $_POST['op1'] ; // there's an error here it should be 'op2' not 'op1' (too fast copy/paste)
              

              by
              $op1 = intval( $_POST['op1']) ; // intval() to be sure the value of op1 and op2 are treated as numbers
              $op2 = intval($_POST['op2']) ;
              


              You can return the value of $value in the error log using
              $modx->log(modX::LOG_LEVEL_ERROR, 'value :' . $value);
              

              and put a specific error message in the form with
              $validator->addError('nameofyourfield', 'the value should be '.$value);
              


              Hope it will work,,let me know
              Cheers
                • 3749
                • 24,544 Posts
                Doesn't FormIt have a math option that will do that automatically?

                There is a mathstring option built in to the captcha plugin package. If you call it from your form, it generates the equation and writes the result to a $_SESSION variable, so all you have to do is check the $_SESSION variable against the user's input in your hook.

                Here is the code from SPForm that displays the math string:

                $captcha_image= '<img '. 'onclick="this.src=' . "'" . $this->modx->getOption('assets_url') . "components/captcha/captcha.php?rand='" .
                            "+Math.floor(Math.random()*200);".'"' .' src="'. $this->modx->getOption('assets_url') . 'components/captcha/captcha.php?rand='.rand().'" alt="'.$alt.'" />';
                
                        $this->modx->setPlaceholder('spf-captcha-instructions',$captcha_prompt);
                        $this->modx->setPlaceholder('spf-captcha-image',$captcha_image);
                        $this->modx->setPlaceholder('spf-captcha-input-prompt',$captcha_input_prompt);
                        $this->modx->setPlaceholder('spf-captcha-input','<input type="text" name="verify"  value="" />');
                
                        $spfCaptchaTpl = $this->modx->getOption('spfcaptchaTpl',$this->spfconfig,'spfcaptchaTpl');
                        $this->modx->setPlaceholder('spf-captcha-stuff',$this->modx->getChunk($spfCaptchaTpl));


                Also, I think you want the math hook ahead of the redirect hook.


                ---------------------------------------------------------------------------------------------------------------
                PLEASE, PLEASE specify the version of MODX you are using . . . PLEASE!
                MODX info for everyone: http://bobsguides.com/modx.html
                  Did I help you? Buy me a beer
                  Get my Book: MODX:The Official Guide
                  MODX info for everyone: http://bobsguides.com/modx.html
                  My MODX Extras
                  Bob's Guides is now hosted at A2 MODX Hosting
                  • 30319
                  • 406 Posts
                  The documentation should describe (and does not) the appropriate sequence for declaring built-in hooks -- another aspect of MODx that maintains its excellent niche status. smiley smiley
                  Thank you everyone, I will try both ideas and try to remember to update this thread.
                  Thank you, smiley tom
                    • 30319
                    • 406 Posts
                    UPdate:
                    sequence of validators is `email, math, redirect` -- this works correctly
                    upon fixing the typo and adding in the intval things work properly
                    I added at the very top this line:

                    if (trim($value) == "") return true; // exit if blank text box


                    Thank you again everyone for your help...

                    :) smiley smiley tom
                      • 3749
                      • 24,544 Posts
                      Quote from: TomMLS at Mar 25, 2012, 12:35 AM
                      The documentation should describe (and does not) the appropriate sequence for declaring built-in hooks.

                      It does now. wink


                      ---------------------------------------------------------------------------------------------------------------
                      PLEASE, PLEASE specify the version of MODX you are using . . . PLEASE!
                      MODX info for everyone: http://bobsguides.com/modx.html
                        Did I help you? Buy me a beer
                        Get my Book: MODX:The Official Guide
                        MODX info for everyone: http://bobsguides.com/modx.html
                        My MODX Extras
                        Bob's Guides is now hosted at A2 MODX Hosting