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 takes some database connection details, uses standard validators and then uses the information collected to test that the entered values are valid mysql connection details using a customvalidator which tries to establish the mysql connection and flags as an error if the connection fails.

    So far I have the following code:

    The form:
    [[!FormIt?
       &hooks=`redirect,install_bash_script`
       &redirectTo=`20`
       &store=`1`
       &zen_inst_db_server=`[[+fi.dbserver]]`
       &zen_inst_db_name=`[[+fi.dbname]]`
       &zen_inst_db_user=`[[+fi.dbuser]]`
       &zen_inst_db_u_pass=`[[+fi.dbpassword]]`
       &customValidators=`install_validation`
       &validate=`dbserver:required,
                  dbname:required,
                  dbuser:required,
                  dbpassword:required,
                  custom_validation_flag:install_validation`
    ]]
    <p>Database Connection test</p>
    <p> </p>
    [[!+fi.error.error_message:notempty=`
    <p>[[!+fi.error.error_message]]</p>
    `]]
    <form class="form" action="[[~[[*id]]]]" method="post">
    <input name="formid" type="hidden" value="zenInstall" />
    <input name="custom_validation_flag" type="hidden" value="1" />
    [[!+fi.error.custom_validation_flag]]
    <fieldset>
    <legend>Database Details</legend>
    <p>
    	<label>Database Server</label>
    [[!+fi.error.dbserver]]
    	<select id="dbserver" name="dbserver">
            <option value="mysql.ourdomain.co.uk" [[!+fi.dbserver:FormItIsSelected=`mysql.ourdomain.co.uk`]]>mysql.ourdomain.co.uk</option>
             <option value="hotel.ourdomain.co.uk" [[!+fi.dbserver:FormItIsSelected=`hotel.ourdomain.co.uk`]]>dev-mysql.ourdomain.co.uk</option>
             </select></p>
    <p>
    	<label>Database Name:</label>
    [[!+fi.error.dbname]]
    	<input id="dbname" class="field" maxlength="40" name="dbname" size="40" type="text" value="[[!+fi.dbname]]" />
    </p>
    <p>
    	<label>Database Username:</label>
    [[!+fi.error.dbuser]]
    	<input id="dbuser" class="field" maxlength="40" name="dbuser" size="40" type="text" value="[[!+fi.dbuser]]" />
    </p>
    <p>
    	<label>Database Password:</label>
    [[!+fi.error.dbpassword]]
    	<input id="dbpassword" class="field" maxlength="40" name="dbpassword" size="40" type="text" value="[[!+fi.dbpassword]]" />
    </p>
    </fieldset>
    <input name="submit" type="submit" value="Run Script" />
    </form>
    
    


    The custom validator (install_validation) snippet:
    <?php
      $db_server = $modx->getOption('zen_inst_db_server',$scriptProperties);
      $db_name = $modx->getOption('zen_inst_db_name',$scriptProperties);
      $db_u_name = $modx->getOption('zen_inst_db_user',$scriptProperties);
      $db_u_pass = $modx->getOption('zen_inst_db_u_pass',$scriptProperties);
     
      
      $db_server=(string)$db_server;
      $db_u_name=(string)$db_u_name;
      $db_u_pass=(string)$db_u_pass;
    
      
      $zdb_server = ereg_replace("[^A-Za-z0-9\. ]", "", "$db_server");
      echo $zdb_server;
      
    //check db
    $link = @ mysql_connect($db_server,$db_u_name,$db_u_pass);
    
        if (!$link) {
          $validator->addError('custom_validation_flag','Database Connection Problem');
          echo mysql_error();
          return false;
        }else{
          mysql_close($link);
        }
    
    return true;
    


    The issue I seem to have is the passing of the field values to the custom snippet.
    The code I currently have is assigning the text string ’fi.dbserver’ to the $db_server variable rather than the actual value of the dbserver field as entered by the user.
    Echoing out the $db_server variable to the screen looks correct but I think this is because modx is parsing [[+fi.dbserver]] before printing to the screen - to prove $db_server is incorrect I have manipulated it slightly to stop modx parsing it (this can be seen by my echo’ing of the $zdb_server variable). Obviously I need the $db_server value to hold the selected host name in order for the mysql connection code to work.

    Any ideas anyone?
      • 8869
      • 32 Posts
      just giving this a gentle bump as it has had no responses ar all sad
        • 8869
        • 32 Posts
        Please can someone have a go at helping me sad

        Someone out there must be able to show off their skills to solve this... just giving the post a little nudge to hope someone clever out their comes to my rescue
          • 8869
          • 32 Posts
          The answer was to ignore customValidator as an approach and just do all validation in a hook using...

          Accessing the FormIt fields in the Hook

          The Formit fields are available via the hook api. Example:

          $email = $hook->getValue(’email’);
          $allFormFields = $hook->getValues();

          If you want to set fields, however, you’ll need to access them this way:

          $hook->setValue(’email’,’[email protected]’);
          $hook->setValues(array(
          ’name’ => ’Shaun’,
          ’books’ => ’Hunger Games,To Kill a Mockingbird,Mindset’,
          ));

          Custom hook return values

          Snippets should return true on success. On failure, the snippet can set error messages in the hook object’s errors variable and return false. In either case, hooks listed after the custom hook in the &hooks parameter will not execute.

          The fiHooks object is available in the snippet as $hook, which can be used to return generic error messages from the snippet:

          $errorMsg = ’User not found’;
          $hook->addError(’user’,$errorMsg);
          return false;
          Again, remember - if your hook succeeds, make sure you have "return true;" at the end of your Hook. If you use "return false;" or do not return a value, FormIt will assume the Hook failed.