We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 36527
    • 120 Posts
    I have a scenario where basic form functions need extending based on choices of a checkbox or radio selection.

    There will be about 20 questions in the actual form, that all will require validation.

    Example below:

    1. Do you require a goat? YES/NO - (checkbox or radio)

    If NO - please explain why - (plain text field for entry of why the choice of NO)

    So what is required as you can see is by ticking a checkbox or selection a radio choice of NO, then the text field becomes active for the user to enter data.

    How would you handle this in the actual eform form field set up and then made sure the eform report accurately reported what was chosen and what was filled in for the textbox, IF it was actually filled in. And of course the validation!

    Any ideas or examples would be great.

    TSMIA





      • 5340
      • 1,624 Posts
      You need a custom validation function. Something like

      <?php
      function reqFields(&$fields, &$vMsg, &$rMsg) {
      	global $modx;
      		
      		if($fields['goat'] == 'no'){
      			if(!$fields['why']){
      				$rMsg['why'] = "No Why";	
      			}
      		}elseif($fields['goat'] == 'yes'){
      			//nothing to do or whatever you wanna do
      		}else{
      		        $rMsg['goat'] = "Goat Error";
      			return false;
      		}
      
      	return true;
      }
      ?>
      
      


      put this is a snippet and call it before your eform like

      [!checkRequiredFields!][!eForm? .......

      Se more in the documentation inside eForm
        • 30223
        • 1,010 Posts
        A small correction.
        <?php
        function reqFields(&$fields, &$vMsg, &$rMsg, &$rClass) { //<-- $rClass holds assoc. array of class names for fields (for eForm > 1.4.4)
        	global $modx;
        
        		/* optional. load parameter values for error css classes */
        		$req_class_name = $modx->event->params['requiredClass'];
        		$inv_class_name = $modx->event->params['invalidClass'];
        		/* end optional */
        		if($fields['goat'] == 'no'){
        			if(!$fields['why']){
        				$rMsg['why'] = "Why";  //<-- should be the field name or desciption only
        				$rClass['why'] .= $rClass['why'] ? ' ' . $req_class_name : $req_class_name; //optional.
        			}
        		}elseif($fields['goat'] == 'yes'){
        			//nothing to do or whatever you wanna do
        		}else{
        		        $vMsg['goat'] = "Goat Error";  //<-- use $vMsg not $rMsg. Add a full error message
        		        $rClass['goat'] .= $rClass['goat'] ? ' ' . $inv_class_name : $inv_class_name; //optional.
        			///return false; <-- do not return false as this will produce a blank screen
        		}
        
        	return true;
        }
        ?>
        


        As xpix said place it in a snippet but call it like thus:
        [[checkRequiredFields]][!eForm? &eFormOnValidate=`reqFields` ... !]

        Edit: removed typo
          • 7231
          • 4,205 Posts
          Quote from: Moondawgy at Feb 10, 2009, 01:31 PM

          1. Do you require a goat? YES/NO - (checkbox or radio)

          If NO - please explain why - (plain text field for entry of why the choice of NO)
          LOL What is the goat for?
            [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

            Something is happening here, but you don&#39;t know what it is.
            Do you, Mr. Jones? - [bob dylan]
            • 20413
            • 2,877 Posts
            @dev_cw: Maybe its this kind: http://www.youtube.com/watch?v=we9_CdNPuJg laugh
              @hawproductions | http://mrhaw.com/

              Infograph: MODX Advanced Install in 7 steps:
              http://forums.modx.com/thread/96954/infograph-modx-advanced-install-in-7-steps

              Recap: Portland, OR (PDX) MODX CMS Meetup, Oct 6, 2015. US Bancorp Tower
              http://mrhaw.com/modx_portland_oregon_pdx_modx_cms_meetup_oct_2015_us_bancorp_tower
              • 36527
              • 120 Posts
              LOL thanks guys for the reply - as for the goat, just a random thing to use as an example tongue

              Code worked a treat thanks all!
                • 5340
                • 1,624 Posts
                ///return false; <-- do not return false as this will produce a blank screen

                Good to know. Thx TobyL.
                  • 29774
                  • 386 Posts
                  @TobyL

                  my testing shows that the $params array is not available from the $modx->event object, when using eFormOnValidate with the latest version of eform.

                  eg try var_dump ($modx->event) inside reqFields().

                  So this doesn’t work:
                  $req_class_name = $modx->event->params[’requiredClass’];
                  $inv_class_name = $modx->event->params[’invalidClass’];

                  Any idea how to get the invalid and required class names without hardcoding them in the function?

                    Snippets: GoogleMap | FileDetails | Related Plugin: SSL
                    • 28042 ☆ A M B ☆
                    • 24,524 Posts
                    Indeed, although the principles are the same, eForm events are not MODx events; plugins cannot be used with eForm and the $modx event object is not available. eForm has its own event model and its own event object that is passed to the functions you create to use its events. Usually a $fields and a $templates array are available.
                      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
                      • 30223
                      • 1,010 Posts
                      @Susan, I beg to differ.... the modx->event->params array is filled with all the snippets parameters in the documentParser evalSnippet() function:
                      <?php
                      function evalSnippet($snippet, $params) {
                              $etomite= $modx= & $this;
                      
                              $modx->event->params= & $params; // store params inside event object
                              if (is_array($params)) {
                                  extract($params, EXTR_SKIP);
                              }
                      .......
                      


                      @therebechips
                      Perhaps you are missing Global $modx; inside your function? I did the test below and it certainly worked for me...

                      Snippet calls:
                      [!showEventParams!]
                      [!eForm? &formid=`test_1` &tpl=`test1` &thankyou=`test1Report` &noemail=`1`  &eFormOnValidate=`showEventParams`!]


                      showEventParams snippet.
                      <?php
                      if( !function_exists('showEventParams') ) {
                          function showEventParams(&$fields){
                              global $modx;
                              $fields['event_params'] ='<pre>'.print_r($modx->event->params,true).'</pre>';
                              return true;
                          }
                      }
                      
                      return '';
                      ?>


                      Form and report template:
                      [+validationmessage+]
                      --------form---------------
                      <form method="post" action="[~[*id*]~]" id="test_1">
                          <p><label for="name">Name </label> <input type="text" name="fullname" size="30" maxlength="60" eform="Name::1" /></p>
                          <p><input class="verify" type="submit" name="Submit" value="Send"></p>
                      </form>
                      --------report-------------
                      <p>Here's the form results:</P
                      <p>Name: [+fullname+]</p>
                      <p>[+event_params+]</p>


                      Results:
                      Here's the form results:
                      
                      Name: TobyL
                      
                      Array
                      (
                          [snipPath] => D:/www/eFormDev/assets/snippets/eform/
                          [snipFolder] => eform
                          [vericode] => 
                          [formid] => test_1
                          [from] => [email protected]
                          [fromname] => Modx CMF demo
                          [to] => [email protected]
                          [cc] => 
                          [bcc] => 
                          [subject] => 
                          [ccsender] => 0
                          [sendirect] => 0
                          [mselector] => 0
                          [mobile] => 
                          [mobiletext] => 
                          [autosender] => 
                          [autotext] => 
                          [category] => 0
                          [keywords] => 
                          [gid] => 64
                          [noemail] => 1
                          [saveform] => 1
                          [tpl] => test1
                          [report] => 
                          [allowhtml] => 0
                          [replyto] => 
                          [language] => english
                          [thankyou] => test1Report
                          [isDebug] => 3
                          [reportAbuse] => 
                          [disclaimer] => 
                          [sendAsHtml] => 
                          [sendAsText] => 
                          [sessionVars] => 
                          [postOverides] => 0
                          [eFormOnBeforeMailSent] => 
                          [eFormOnMailSent] => 
                          [eFormOnValidate] => showEventParams
                          [eFormOnBeforeFormMerge] => 
                          [eFormOnBeforeFormParse] => 
                          [cssStyle] => 
                          [jScript] => 
                          [submitLimit] => 0
                          [protectSubmit] => 1
                          [requiredClass] => required
                          [invalidClass] => invalid
                          [runSnippet] => 
                          [autoSenderName] => 
                          [version] => 1.4.4
                      )