We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
  • I have this (partial) form:

    [[!FormIt? &hooks=`myHook,spam,email,redirect` &emailTpl=`MyEmailChunk` &emailTo=`[email protected]` &redirectTo=`1`]]
    
    <form class="form" action="[[~[[*id]]]]" method="post">
    <input name="aanmelding" type="radio" value="ouders" [[!If? &subject=`[[+fi.aanmelding]]` &operator=`=` &operand=`ouders` &then=`checked="checked"`]] />
    <input type="text" name="aanmelding-ouders" size="27" value="[[+fi.aanmelding-ouders]]" [[!If? &subject=`[[+fi.error.aanmeldingouders]]` &operator=`notempty` &then=`class="error"`]] />
    <input type="submit" />
    </form>
    


    My ’myHook’ snippet:
    <?php
     $scriptProperties['hook']->errors['aanmeldingouders'] = 'test';
    return false;
    ?>
    


    Why doesn’t [[!If? &subject=`[[+fi.error.aanmeldingouders]]` &operator=`notempty` &then=`class="error"`]] work? The error should always be there whith this code???

    Offcourse I have the If snippet installed (which is working allright).
      Jeroen Kenters

      MODX Professional | MODX Ambassador | Dutch MODX forums moderator

      website | twitter
    • A couple things:

      Are those ampersands HTML entities in your Snippet call? Or did they just get converted to HTML entities (&amp ;’s) when you posted them here?

      Generally, I don’t think the approach you’re taking is the best one... the If statement Snippet could be to blame. For your solution to work, it would require that the 2 Snippets execute in a certain order and one Snippet would have to be aware of the output by the other. Maybe it works... but it makes my head hurt... that’s fine INSIDE a Snippet, but on your page (i.e. inside your View layer) that could be a sign of a bad approach.

      Could you restructure this to utilize an Output Filter instead? http://rtfm.modx.com/display/revolution20/Input+and+Output+Filters
      • Changed, but without luck.

        form:
        [[!FormIt? &hooks=`myHook,spam,email,redirect` &emailTpl=`MyEmailChunk` &emailTo=`[email protected]` &redirectTo=`1`]]
        <form class="form" action="[[~[[*id]]]]" method="post">
        
        <input type="text" name="myfield" value="[[+fi.myfield]]" class="[[+fi.error.mycheck:notempty=`error`]]" />
        
        <input type="submit" />
        </form>
        


        validation hook that should always fail:
        <?php
        $scriptProperties['hook']->errors['mycheck'] = 'test';
        return false;
        ?>
        
          Jeroen Kenters

          MODX Professional | MODX Ambassador | Dutch MODX forums moderator

          website | twitter
          • 1778
          • 659 Posts
          Hello

          Why do you use a hook in this case, as I understand it, what you want is to display an error message if the field "myfield" is empty, am I right ? If yes you don’t need a hook to do this, but a custom validator...

          Cheers
          • I simplified this form. In reallity this form is very complex. If you select an option some fields need to be checked. Otherwise they can be left alone.

            However, this problem is ’solved’. My custom hook is only activated after all built-in validation is done. I didn’t realise that. It’s not completely what I want, but it will do for now.
              Jeroen Kenters

              MODX Professional | MODX Ambassador | Dutch MODX forums moderator

              website | twitter
              • 1778
              • 659 Posts
              Hello

              I had a form like that if the checkbox X is checked you must fill in text fields #1 and #2, if one option is selected in an option list you have to fill in the text field #3etc,...

              I did it only with custom validators... It works fine too... ;-) glad you had found a solution... The good side of Modx there’s always multiple ways to fit a goal...

              Cheers
              • That sounds right... the FormIt Snippet does its thing (including validation), then it starts calling the hooks, one by one.

                In your custom hook Snippets (or in any Snippet), you can get a lot more visibility if you use MODx logging, e.g.

                $modx->log(modX::LOG_LEVEL_ERROR, "MySnippet was missing an argument for &something.");


                It’s a better way of tracing errors/info than using a print statement.