We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 9439
    • 90 Posts
    I'm reposting this as I added it on to my previous post and the title of that post - to do with postcodes - doesn't really reflect the nature of the problem. Which is:

    My form looks up and validates a bank sort code and account number. It works fine as a standalone php form but it's not clear to me how to use formit and a snippet to achieve the same result. (ultimately, I want to use formit to email all the values as a spreadsheet - which works just fine as well)

    The form, stripped down, is:

    <label for="sortcode">Sort Code<span class="error">[[!+fi.error.sortcode]]</span></label>
      <input type="text" name="sortcode" value='<?php if(isset($_POST['sortcode'])) { echo htmlentities ($_POST['sortcode']); }?>' /> // set to retain the input in the input field
     
    <label for="AccountNumber">Account Number</label>   
       <input type="text" name="AccountNumber" value='<?php if(isset($_POST['AccountNumber'])) { echo htmlentities ($_POST['AccountNumber']); }?>' />   // set to retain the input in the input field
       <input class="button" name="click" type="submit" value="Check bank account details" />   
    


    PHP

    if(isset($_POST['click']))
    {
       $sortcode = $_POST['sortcode'];
       $AccountNumber = $_POST['AccountNumber'];
    .
    > external database call, which returns array
    .
    $data = $pa->HasData();
       foreach ($data as $item)
          {
            $varVerifyAccount = $item["AccountVerification"];
    more items from array...
    


    So the external call checks the account and we use $varVerifyAccount to advise the user whether or not the account may be used. You can see how the form looks here: http://pfsmedia.net/letusfixit/account-form.php

    Can someone give me a steer how to code the snippet? Also, you can see I'm keen to retain the values in the input fields after submit is clicked. Not sure how to do that in formit either.

    Thanks
      David Heriot
      pfsmedia
      • 3749
      • 24,544 Posts
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 9439
        • 90 Posts
        Quote from: BobRay at Jul 11, 2016, 04:01 AM
        See if this helps: https://rtfm.modx.com/extras/revo/formit/formit.validators#FormIt.Validators-CustomValidators

        Thanks Bob, that should help with validation part at least. Then I found that I couldn't even write to a chunk from the output of the form. It seems very straightforward but each time the placeholders are frustratingly empty. Even stripped down to bare essentials I still couldn't get anything to display. I just can't see what's wrong.

        Snippet

        $formit =& $hook->formit; //looks odd this line but copied from a working snippet
        
        $uname = $hook->getValue('username');
        $num = $hook->getValue('active');
        
        //$formValues = $hook->getValues();
        $output = '';
        $modx->log(xPDO::LOG_LEVEL_ERROR,'check snippet trigger');
        
            $output = $modx->getChunk('myChunk', array(
                'name' => $uname,
                'number' => $num
            ));
        
        return $output;


        Chunk (in resource 10)

        <p>Username: [[+name]]<br/>Active:  [[+number]]</p>


        Formit call (in resource 17)

        [[!FormIt? 
           &hooks=`mySnippet, redirect`
           &redirectTo=`10`]]
          David Heriot
          pfsmedia
          • 3749
          • 24,544 Posts
          I'm not sure if this is your main problem, but Hooks need to return true. FormIt would not process text returned from a hook. It would most likely just abort if the hook didn't return true.

          Try this:

          On the page:

          [[+hook_output]]



          In the snippet:

              $output = $modx->getChunk('myChunk', array(
                  'name' => $uname,
                  'number' => $num
              ));
          
              $modx->setPlaceholder('hook_output', $output);
           
          return $true;
          



          Be sure the placeholder is below the FormIt tag.
            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting
            • 9439
            • 90 Posts
            Thanks again Bob. Such a small thing, but probably the only thing I didn't try, in spite of my searching.

            So the chunk only formats the output, and formit hooks need a return true? Got it. Now back to the API and see if I can put this into practice.
              David Heriot
              pfsmedia
              • 3749
              • 24,544 Posts
              Right. Unlike a snippet, if you need to display output from a hook, you need to set a placeholder in the hook.

              It makes sense if you think about it. A snippet tag is replaced by the return value of the snippet, but with a hook, there's no tag to replace, so you have to create a placeholder tag and set the placeholder in the hook.

              I should also mention how the return value of a hook is used. If you have multiple hooks and one returns false, the hooks after it on the list won't execute. That's why it's standard to return true. If you only have one hook, I'm not sure if it matters, but it's still a good practice to return true, since you might decide to add more hooks later. [ed. note: BobRay last edited this post 7 years, 9 months ago.]
                Did I help you? Buy me a beer
                Get my Book: MODX:The Official Guide
                MODX info for everyone: http://bobsguides.com/modx.html
                My MODX Extras
                Bob's Guides is now hosted at A2 MODX Hosting
                • 9439
                • 90 Posts
                Thanks Bob. I take it that all this stuff's in your book? wink
                  David Heriot
                  pfsmedia
                  • 3749
                  • 24,544 Posts
                  TBH, I had to check to see, but yes the book does have a section on FormIt custom hooks. I think the error return details might have changed since then, but most of it is still good. When I wrote it, you could return an error message as a string. I'm not sure that still works, but false and true returns definitely still work.
                    Did I help you? Buy me a beer
                    Get my Book: MODX:The Official Guide
                    MODX info for everyone: http://bobsguides.com/modx.html
                    My MODX Extras
                    Bob's Guides is now hosted at A2 MODX Hosting
                    • 9439
                    • 90 Posts
                    And just in case anyone else is grappling with assigning a chunk with the values from a SimpleXMLElement Object, it's like this. Add (string) to the var. I just put it here because I've just spent too long on it myself sad

                    $output = $modx->getChunk('myChunk', array(
                            'name' => (string)$uname,
                            'number' => $num
                        ));
                     
                        $modx->setPlaceholder('hook_output', $output);
                      
                    return $true;
                    
                    
                      David Heriot
                      pfsmedia