We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 20751
    • 122 Posts
    I have two fieldsets each with about 5 input fields.

    The first fields in the first fieldset are all required fields. Is there some way I can hack eform so that if just one of any of the 5 input fields is filled out in the second fieldset, then they all become required fields too?

    I know this may be a long shot but if anyone has any ideas how I might go about it, I’d much appreciate it!

    Thanks!

      • 30223
      • 1,010 Posts
      Set those fields all to be non-required, write yourself a special validation function which validates the 5 fields according to what you described, place this function in a snippet and set the eFormOnValidate to this function.

      In the eForm documentation or in this post you can read how to include the function so that eForm can access it.

      good luck
        • 20751
        • 122 Posts
        Yes, looking throught the docs, I figured this might be the way forward. I’ve been up all night figuring a way to do it but nothing works yet. If I get it working I’ll post my results!

        Thanks for the tip/post reference!

        :)
          • 20751
          • 122 Posts
          OK. I’m stuck . . .

          I’ve created a snippet called eFormValidation with a function inside called validateFields which looks like this:

          <?php
          # Snippet: eFormValidation
          
            function validateFields(&$fields,&$vMsg,&$rMsg){
            
            global $_lang;
            global $name;
          
              if ($name['meter2_1'] == "") {
                $rMsg[count($rMsg)]="$desc";
          	  $rClass[$name]=$requiredClass;
              }	
          
            return true;
          
            }
            
          ?>


          I’m calling the above snippet right before my eform snippet and using &eFormOnValidate=`validateFields` in my eform snippet.

          So, if meter2_1 is missing, I get an error but only if I write in my own error message where $desc is. How do I populate the $desc variable in my function and how do I get the required class to work too? I’ve tried making $desc global but it doesn’t seem to work.

          Thanks in advance!

            • 30223
            • 1,010 Posts
            I’m not sure where you get the $name variable from but I’d be very surprised if that contained anything useful in this case. The problem you’re having with the variables you are trying to use is that they are no longer available or can not be brought into the function’s scope (without altering the eform core). $desc does not exist by the time the function is called. $rMsg is never within the scope of the function. This is unfortunate in your case but without hacking the eform code itself there’s no way around it.

            Below is a more or less generic function that will test if any of the declared fields (see comments in code) have been filled in and if at least one but not all are filled in set an error.

            <?php
            # Snippet: eFormValidation
            
              function validateFields(&$fields,&$vMsg,&$rMsg){
            
               //declare optional fields (name => description)
               //set these to your form fields that you want tested
                  $optionalFields = array(
                     'name2_1' => 'Name 2 Desc',
                     'field2' => 'Field 2',
                     'field3', => 'Field 3',
                     'field4', => 'Field 4',
                     'field5' => 'Field 5'
                  );
                  //step through the fields, count how many are missing
                  foreach($optionalFields as $ofield => $oDescr){
                     if($fields[$ofield] !=''){
                        $filledInFields++;
                     else
                        $missingFields[] = $oDesc;
                  }
                  //if at least 1 field is filled in but not all
                  //
            
                  if($filledInFields > 0 && $filledInFields < count($optionalFields)){
                     $vMsg['Optional Fields'] = 'You have only filled in some of the optional fields. You must also fill in' . implode(', ',$missingFields);
                     
                     //this only works if you can bring the $rClass into scope
                     //you'll have to hack the eform code to do this
                     //either by making the $rClass global (both in eform and here)
                     //or by adjusting (including it in) the event call and reference it in this function decalration
                     foreach($missingFields as $ofield){
                        $rClass($ofield) = 'required' //make same as &requiredClass (as set in snippet call)
                     }
                     //--//
                  }
            
                 return true;
               }
              
            ?>
            
              • 20751
              • 122 Posts
              Oh my . . . I can’t believe you did that. Thank you so very much. This worked perfectly!!!

              You’re awesome!!

              grin grin grin