We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 22303 MODX Staff
    • 10,725 Posts
    I plan on making this a module at some point, but want to go ahead and release this as a helper class with a sample snippet to see if anyone finds it useful before deciding on how it should evolve.

    I used this and another class I released recently, MakeTable, to replace three different extranet sites that included different views into the same database tables. The sites were originally built with a commercial RAD development tool called CodeCharge Studio, but I convinced the client I could do it better with MODx. I came up with MakeTable and MakeForm because I had some 45 different tables and forms to reproduce (and fix), and the data structures I had to work with would make any serious database administrator spontaneously combust at first sight. The project was a huge success, and changes to the forms rarely take more than 5 minutes of my time, and data structures changes can be handled in minutes rather than hours.

    What is MakeForm?
    MakeForm helps you quickly build forms for adding/editing records in a database. It does this using three major components:


    • Form arrays. First, every form element is represented by an array. This array defines all of the placeholder values that can be used inside the next key component.

    • Form element templates. Each element type uses a chunk as a template, so you can customize the way each element type is generated.

    • Form processing. You can easily process POST form data against a specified database table using MakeForm and some new DBAPI features new to MODx 0.9.1. You can see these new features used in the source of the makeform.class.inc.php file.

    How do I check it out?
    I’ve provided a sample snippet (this is meant to be viewed in the MODx default sample template and might need some additional CSS to be viewed properly in other templates/styles as a result), a set of form element template chunks, and the class in the attached zip. This needs to be extracted into the /assets/modules/makeform/ directory (like I said, I plan to make it a module eventually). Once that is done, you’ll need to take the following steps to view the sample:


    • First, copy the contents of each one of the MakeForm-*.chunk.html files to a chunk, naming it after the filename, but without including the .chunk.html suffix. [NOTE: I add these suffixes to identity the type of MODx object it is and the type of content so I can edit it properly in an IDE]
    • Create another chunk named sampleFormTpl and copy the contents of the sampleFormTpl.chunk.html into it
    • Next create a new snippet called MakeForm-Sample with the following content:
    return include($modx->config['base_path'] . "assets/modules/makeform/MakeForm-Sample.snippet.php");


    • Finally, create a new non-cacheable document in MODx and copy the content from the MakeForm-Sample.document.html file into your new document’s content editor (use HTML source mode if you use an RTE, for best results wink).

    When you visit the page you created in the last step, assuming the database user you are using to connect to MODx has permissions to create a new database table in the MODx database, the table will be created if it does not exist, and it should display a form (or a bunch of errors in a red background grin).

    Let me know if you find this useful or full of errors.

    [UPDATED: Corrected MakeForm-InputSubmit.chunk.html and MakeForm-InputButton.chunk.html with name=[+name+] as reported by sottwell]
      • 6726
      • 7,075 Posts
      Waow I love the concept, everyday is a day full of surprises @MODx !!! Thanks Jason grin

      I’ll test it as soon as I get a few minutes free...
        .: COO - Commerce Guys - Community Driven Innovation :.


        MODx est l'outil id
        • 28042 ☆ A M B ☆
        • 24,524 Posts
        The included snippet still has the <?php at the top; no big deal to remove it.

        I’m not sure what it’s doing...I get an input with the label "Name" and a (very wide) checkbox with the label "Disabled".

        I see that it did create the table "samples", although it’s not getting anything put into it. Will explore further.
          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
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          Looks like an error in assigning the "name" of the submit button:

          <input type="submit" id="sample-save" name="value="Save  value="Save" style="width:90px;" />


          In the MakeForm-InputSubmit chunk, it has

          name="[+value+]"


          it should be

          name="[+name+]"


          and that fixes it.
            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
            • 1764
            • 680 Posts
            Very cool Jason. I was thinking about writing something similar to this myself. I’ll have to give it a look. Thanks.
              • 32241
              • 1,495 Posts
              It really is cool indeed.

              Thanks for this great class. I just added this to the repository smiley
                Wendy Novianto
                [font=Verdana]PT DJAMOER Technology Media
                [font=Verdana]Xituz Media
                • 22303 MODX Staff
                • 10,725 Posts
                Quote from: sottwell at Jan 26, 2006, 04:04 AM

                The included snippet still has the <?php at the top; no big deal to remove it.

                I’m not sure what it’s doing...I get an input with the label "Name" and a (very wide) checkbox with the label "Disabled".

                I see that it did create the table "samples", although it’s not getting anything put into it. Will explore further.

                I always keep my snippets in php files so I can edit them and debug them in an IDE. All of my snippets in the database are this one line of code that includes this PHP file. Just the way I roll...

                It’s not doing much but building a blank form for a new samples record to be entered, when you submit, it persists to the database, and then you are editing that record; optionally, you can use ?sample=x as a GET parameter, where x is the sample.id to pull up a specific sample record for editing.

                I’ll try and expand the example to include at least one of each form element type somtime soon. And blame the default checkbox style on the default template CSS. ;-)

                And good catch on the submit template -- I had been experimenting with some things and forgot to change that chunk back to the proper values.
                  • 28042 ☆ A M B ☆
                  • 24,524 Posts
                  Yes, of course...I am spoiled because my editor does not require that.

                  Once I actually got it working, I understood that it’s all in the snippet. Somehow I had gotten the idea that the snippet would put up a form to let me create the form I wanted. All clear now, and very nice. Now to bolt on some AJAX...
                    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
                    • 22303 MODX Staff
                    • 10,725 Posts
                    And BTW, you can add any array keys to the elements you want to be rendered as:

                    {key}="value"


                    So you can easily add onclick or onchange or whatever events you want via the templates and the array elements.
                      • 11975
                      • 2,542 Posts
                      Hi,

                      I’ve been trying for several days to setup a form according to my needs.
                      But until now I don’t get the right result to include Javascript events.

                      Here is the correct output I’d like
                      <input type=’checkbox’ name=’langue[]’ value=’0’ onclick="checkBox(’langue’);" />

                      Coul you please post a sample code to call events with makeForm Class.

                      Thank you smiley
                        Made with MODx : [url=http://www.copadel.com]copadel, fruits et l