We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 23420
    • 6 Posts
    Is there a way to make a a field become required if another radio or checkbox is selected? For example, when someone checks a checkbox they then have to fill in a required text field but if they leave it unchecked they do not.
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      You would probably need a custom validation function to be triggered by the &eFormOnValidate event; it provides three parameters for your function
      &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.
      So you could get the values of your checkbox, and if it’s checked then check the value of the dependent field, setting your own vMsg or rMsg array values as necessary.
        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
        • 36805
        • 354 Posts
        Encouraged by Susans idea I have spent almost a full day experimenting. Unfortunately with no success at all.

        I use &eformOnValidate to call my own validation function. I include it as a snippet like the docs suggested. I use modx system event logging to check if my validation runs. In reports > system events I can see it all runs as expected.

        If I load the page I see my form and the debug info. After submit nothing shows up, regardless of my return value. If i remove my &eformOnValidate parameter from the call it works again.

        I’d appreciate some help. At this point I completely ran out of ideas and I wonder whether it is possible at all. huh

        my form chunk "eFormSignup"
        <span style="color: crimson">
        [+validationmessage+]
        </span>
        <form action="[(base_url)][~[*id*]~]" method="post">
        	<input type="hidden" name="formid" value="eFormSignup" />
        	<fieldset>
        		<legend>Registrieren</legend>
        		<p>
        			<label>Email</label>
        			<input class="field" type="text" name="email1" size="40" maxlength="40" />
        		</p>
        		<p>
        			<label>Repeat Email</label>
        			<input class="field" type="text" name="email2" size="40" maxlength="40" />
        		</p>
        		<p>
        			<label>Password</label>
        			<input class="field" type="text" name="pass1" size="40" maxlength="40" />
        		</p>
        		<p>
        			<label>Repeat Password</label>
        			<input class="field" type="text" name="pass2" size="40" maxlength="40" />
        		</p>
        		<p>
        			<input type="submit" name="submit" value="OK" />
        		</p>
        	</fieldset>
        </form>

        My snippet ’validateFunctions’
        <?php
        function validPass($pass)
        {
            global $modx;
            $modx->logEvent(998, 1, 'start validPass', 'validPass');
        
            if (strlen($pass) < 5)
            {
                $modx->logEvent(998, 1, 'pass too short', 'validPass');
                return false;
            }
        
            $modx->logEvent(998, 1, 'pass long enough', 'validPass');
            return true;
        }
        
        function validEmail($email)
        {
            global $modx;
            $modx->logEvent(998, 1, 'start validEmail', 'validEmail');
            
            if (strlen($email) < 3)
            {
                $modx->logEvent(998, 1, 'email invalid', 'validPass');
                return false;
            }
            
            return true;
        }
        
        function checkSignup(&$fields, &$vMsg, &$rMsg)
        {
            global $modx;
            $modx->logEvent(998, 1, 'start checkSignup', 'checkSignup');
            
            $errors = 0;
        
            // check password fields
            if ($fields['pass1'] !== $fields['pass2'])
            {
                $modx->logEvent(998, 1, 'passwords dont match', 'checkSignup');
                $pass = $formpass;
                $vMsg['pass1'] = "Passwords dont match";
                $errors++;
            }
            else
            {
                if (!validPass($fields['pass1']))
                {
                    $modx->logEvent(998, 1, 'password rule fail', 'checkSignup');
                    $vMsg['pass1'] = "Password needs to be at least 5 characters long";
                    $errors++;
                }
            }
        
            // check email fields
            if ($fields['email1'] !== $fields['email2'])
            {
                $modx->logEvent(998, 1, 'email2 dont match', 'checkSignup');
                $pass = $formpass;
                $vMsg['email1'] = "Email Adresses dont match";
                $errors++;
            }
            else
            {
                if (!validEmail($fields['email1']))
                {
                    $modx->logEvent(998, 1, 'email rule fail', 'checkSignup');
                    $vMsg['email1'] = "Email Adresse is invalid";
                    $errors++;
                }
            }
        
            if ($errors > 0)
            {
                $modx->logEvent(998, 1, 'errors found, returning false', 'checkSignup');
                return false;
            }
        
            $modx->logEvent(998, 1, 'no errors found, returning true', 'checkSignup');
            return true;
        }
        
        function doSignup(&$fields)
        {
            global $modx;
            $modx->logEvent(998, 1, 'start doSignup', 'doSignup');
            $modx->logEvent(998, 1, 'dummy function returning true', 'doSignup');
            return true;
        }
        
        //return empty string
        return '';
        ?>

        My chunk ’eFormSignupThanks’
        <p>Thanks for signing up for our website.</p>

        My chunk ’eFormSignupReport’
        <p>A new user has signed up</p>
        <table>
        <tr valign="top"><td>Email:</td><td>[+email1+]</td></tr>
        </table>
        

        My snippet calls on the page
        [!validateFunctions!]
        [!eForm? &formid=`eFormSignup` &to=`[email protected]` &tpl=`eFormSignup` &report=`eFormSignupReport` &thankyou=`eFormSignupThanks` &subject=`mydomain.com - A new User just signed up` &protectSubmit=`0` &eformOnValidate=`checkSignup` &debug=`1`!]
          • 36805
          • 354 Posts
          I did get it to work, however it does not behave quite as I expected.

          1. eformOnValidate function cannot return false or you get a blank page (here: function checkSignup(...))
          2. $vMsg array must be set with numeric index or it won’t show (*)

          (*)
          instead of:
          $vMsg[’pass1’] = "Passwords dont match";
          $vMsg[1] = "Passwords dont match";

          will output the error message on the second line of [+validationmessage+] (first line is index 0 and should be avoided, it is reserved for non-custom eForm validation errors)