We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 8869
    • 32 Posts
    Hi

    I’m trying to create a form which gathers multiple values from load of different fields on a form and then perform some validation before processing the form

    The normal validation rules for checking the fields have text entered and of the right format are working fine.

    However what I’m struggling with is having a #FUNCTION which can do some validation based on a combination of the entered fields.

    I have setup a hidden field to pass the #FUNCTION validation but unfortunately it is only passing through the value of the one hidden field and I haven’t managed to work out how to access all the form field values entered

    eg 3 of my fields are:
    db_server
    db_username
    db_password

    And I want the validation rule to double check that combination of the 3 fields allows a valid mysql connection.

    Any ideas?
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      I would use a custom function-in-a-snippet using the eFormOnValidate event.

      # &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 accommodate 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.
        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
        • 8869
        • 32 Posts
        Hi Thanks for this - I now have a function called val_zen been called on the validation event

        However how do I pass a failure or pass back to the form

        So far my code in the function looks like:

        <?php
        //check db
        	function val_zen( &$fields ){
        			$db_server = $fields['dbserver'];
        			$db_u_name = $fields['dbuser'];
        			$db_u_pass = $fields['dbpassword'];
        			
        
        $link = mysql_connect("$db_server", "$db_u_name", "$db_u_pass");
        
        
        		if (!$link) {
        			return false;
        		}else{
        			mysql_close($link);
                                return true;
        		}
        
        }
        ?>


        But on fail its not returning to the form and I don’t understand how to set the an error message to appear on the form telling the user the database connection details they entered are wrong
          • 8869
          • 32 Posts
          Solved it Myself...

          for those that come after me this may be helpful... In
          $vMsg[4] = "Database Connection Problem";
          the number of the array position needs to be the number of form fields plus 1

          <?php
          //check db
          	function val_zen( &$fields, &$vMsg, &$rMsg ){
           global $modx;
          			$db_server = $fields['dbserver'];
          			$db_u_name = $fields['dbuser'];
          			$db_u_pass = $fields['dbpassword'];
          
          $link = @ mysql_connect("$db_server", "$db_u_name", "$db_u_pass");
          
          		if (!$link) {
          			$vMsg[4] = "Database Connection Problem";
          		}else{
          			mysql_close($link);
          		}
          
          }
          ?>