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

    Just a quick question; I want to call a snippet from within a form, my reading of this is that I should simply be amending the form template chunk. I’m aware that chunks do not run PHP code, and therefore can’t run snippets directly so I was hoping for some tips on how to get around this problem?

    I’ve attempted using a TV and including an @EVAL bind in that TV (@EVAL $modx->runSnippet("SnippetName"); ) but this didnt work. I’m sure that running a snippet from a chunk has come up on this forum before, however I can’t seem to find where.

    Any tips appreciated.

    Cheers

    Steve
      • 31037
      • 358 Posts
      Hi again Steve! Seems like we are still doing the same thing both of us tongue

      Are you trying to do something similar as I want to do in this post http://modxcms.com/forums/index.php/topic,7521.msg59726.html#msg59726 ?
      That one I posted almost exactly the same time as your post here tongue
        • 6437
        • 157 Posts
        Hello again smiley

        I don’t believe it is quite the same issue, from my understanding of your post you are trying to pre-populate eForm fields from a database query? This is way beyond me at the moment, although it is something I would like to create in future, to provide "edit" record functionality. I assume this is almost what you’re trying to achieve - to allow a user to edit their information?

        My problem is slightly simpler, I just want a form element that is generated by a php script to be placed within my eForm template, so that it can be completed as usual by the user.

        Steve
          • 31037
          • 358 Posts
          Our problems are more similar than you think! I need to be able to do both these things as need to create form elemens dynamically.

          Actually, you can call a snippet from within a chunk, but you cannot do it from within a eForm tpl chunk. Or you can, but non of us seems to know how!

          Hopefully somone answered our questions when I get back from work!

          Good Luck!

          EDIT: Maybe that’s the answer to my own questions, I might pass the tpl to my script and modifying it there... or maybe I’m totally wrong! As usual!
          I don’t wanna go to work, I want to play with MODx tongue
            • 30223
            • 1,010 Posts
            Quote from: DangerMouse1981 at Oct 31, 2006, 10:46 AM

            Hi all,

            Just a quick question; I want to call a snippet from within a form, my reading of this is that I should simply be amending the form template chunk. I’m aware that chunks do not run PHP code, and therefore can’t run snippets directly so I was hoping for some tips on how to get around this problem?

            With the coming 1.4.1 version which I will post into the repository tomorrow you could achieve this using the eformOnBeforeFormParse event.

            What you would have to do is

            • place a placeholder in the form template where you would otherwise have called the snippet. eg. [+snippetOutput+]
            • create a snippet with a function (eg. function myFunction( &$templates){ ... } ) which does the work you wanted to do (or make it call another snippet with $modx->runSnippet first to get the output). The function should replace the [+snippetOutput+] in the $templates[’tpl’] variable that was passed as a (refrenced) parameter.
            • insert a call to this snippet in the page before the eForm snippet call to make teh function available to eForm
            • In the eForm snippet-call add the &eformOnBeforeFormParse=`myFunction`

            Hope this makes a bit of sense. Once 1.4.1 is in the repository you can read more about the new events in the documentation. At least it is possibel to do what you want, albeit with a bit of creative coding smiley

            If you can’t wait until it’s in the repository you can have a look at this post where there’s a preliminary version for download but be aware that that is not a completely tested version of eForm (the event should work however).
              • 6437
              • 157 Posts
                Thanks TobyL, this is definately what I’m seeking to achieve smiley

                Quote from: TobyL at Oct 31, 2006, 04:34 PM

                The function should replace the [+snippetOutput+] in the $templates[’tpl’] variable that was passed as a (refrenced) parameter. [/li]

              This bit loses me a little (largely because I’m new to php I’m sure) any chance you could expand or provide a simple example please?

              I look forward to seeing the new version in the Repository.

              Thanks

              Steve

                • 30223
                • 1,010 Posts
                I’ll see if I can whip up another example using events but I’ll probably do that in the documentation wiki.

                However, thinking about your problem again and I see no reason why you can’t use a snippet (or a modx tag) directly in the form template chunk. It depends on where you place it. For example if you have a snippet called insertEmail:
                <?php
                return "[email protected]";
                ?>
                

                and you add this to the form template chunk
                email: <input name="email" type="text" value="[[insertEmail]]" eform="Email::1:"/>

                The first time the form is displayed the email field will be filled by the snippet. In this case thi sonly works the first time the form is displayed because when the form is posted eForm will parse the form and use the posted value instead.

                It all depends where you place the snippet call in the form. If it is inside <input>, <textarea> ,<select> or <option> tags then chances are that certain things get stripped by eForm when the form is posted. Otherwise your snippets should just work...

                Here’s another example where a snippet is used to generate option tags:
                Edit: This is actually a bad example!!!
                Because the snippet is not evaluated until after eForm has finished, eForm will not be able to parse the inserted tags and when posting the form it will not see or not validate the values! To insert extra form elements you need to use the eFormOnBeforeParse event. Try this example and you’ll see eForm throws an error when the form is posted.
                <?php
                # insertOptionTags example snippet
                $limit = isset($limit)?$limit:3;
                $output = '';
                for($i=0;$i<$limit;$i++){
                $output .= '<option value="'.$i.'">option '.$i.'</option>';
                }
                return $output;
                ?>
                


                And here’s where it is used in a form.
                <select name="someSelection" eform="Selection Example::1">
                   [[extraOptions? ? &limit=`6`]]
                </select>
                



                By the way, you can also use modx tags in a form template. For instance:
                <input type="submit" name="submit" value="Send this to [(site_name)] "/>



                Edit: as promised I’ve added version 1.4.1 to the repository. See this post for changes and a link to teh rpository
                  • 31037
                  • 358 Posts
                  Steve, don’t give up! I’ve managed to solve my problem with populating the form fields with data from my database, now I have to manage to create form fields dynamically with php code, and from what I understand it is what you also want to do? Or have I misunderstood it? My bad english sometimes make it hard to understand tongue

                  I have an idea how to solve this, must do some testing. I fould a way yesterday similar to what TobyL suggests above. I wrote you a long answer how to greate all the form fileds dynamically in a snippet, but than I noticed that the validation doesn’t work! sad So I throw my post away.

                  But I think I’m close to a solution now that I have the 1.4.1 installed.

                  Good luck!

                  EDIT: Yes! I works! With the version in repository all my solutions I’ve unsuccesfully been working with for latest 20 hours works fine! I’ll post you some example code asap!
                  • FWIW, I’ve used eForm with Snippets in form fields. This was for a file submission form from logged in webusers:

                    [[userDetailName]]
                    $pid = $modx->getLoginUserID();
                    $data = $modx->getWebUserInfo($pid);
                    
                    return $data['fullname'];


                    Form template excerpt:
                    ...
                        <fieldset>
                            <legend>Your personal details</legend>
                            <div>
                                <label>Name</label>       
                                <input class="field" type="text" value="[[userDetailName]]" name="fullName" maxlength="60" eform="Your Name:string:1:Your Full Name is required" />
                            </div>
                            <div>
                                <label>Email</label>       
                                <input class="field" type="text" value="[[userDetailEmail]]" name="email" size="40" maxlength="40" eform="Email Address:email:1" />
                            </div>
                    ...


                    When the user is logged in, their name (and email) is automatically filled in.
                      Ryan Thrash, MODX Co-Founder
                      Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
                      • 6437
                      • 157 Posts
                      Thanks both.

                      That "snippet" of code (no pun intended!) looks really usefull for prepopulating values Ryan.

                      Glad that you’re getting on well Uncle68. You’re quite right in thinking that i’m looking to dynamically generate form fields - well hidden field values to pass on form submit (they’re to be picked up by a function called via another eForm event).

                      At the moment I’m just testing by getting it to insert the contents of any variable into a placeholder (a simple txt string) but can’t seem to master it.

                      Steve