On March 26, 2019 we launched new MODX Forums. Please join us at the new MODX Community Forums.
Subscribe: RSS
  • Working Version of Formit2db and db2FormIt on same form

    1.) I wanted to create records in a custom table using the same form/resource as the editing of the record.
    2.) I have my own custom tables using myprefix_some_table = (crm_merchant_scripts)
    3.) Did not want to redirect to a different page. I want to stay on same page and show success message (better user experience in IMO instead of having to go "back"
    4.) i can create records and edit them easily when using redirects, but when staying on the same page db2FormIt did not seem to pull the data so I had to use FormItRetriever. (even though the request param was there.


    This is working, but I'm wondering if there is a better way to do this? (using the same form to create and edit without redirect.)

    Here is my Snippet Call:
    [[!FormIt?
      		&preHooks=`db2FormIt`
    		¶mname=`scriptid`
    		&fieldname=`id`
    		&hooks=`Formit2db`
    		&successMessage=`<div class="alert alert-success alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button><p class=""><i class="fa fa-thumbs-o-up fa-fw"></i> Successfully submitted form.</p></div>`
            &prefix=`crm_`
            &packagename=`crm`
    
    		&where=`{"id":[[!+fi.scriptid:eq=``:then=`[[!+fi.id]]`:else=`[[!+fi.scriptid]]`]]}`
            &errTpl=`<span class="label label-danger">[[+error]]</span>`
            &classname=`MerchantScripts`
            &tablename=`MerchantScripts`
    		&validate=`script_name:required`
    		&store=`1`
    	 ]]
    
    	 [[!FormItRetriever]]
    
         <form method="post" action="[[~[[*id]]]]?scriptid=[[!+fi.id]]" role="form">
    	  <input name="scriptid" value="[[!+fi.scriptid:eq=``:then=`[[!+fi.id]]`:else=`[[!+fi.scriptid]]`]]" type="hidden">
    ... (other form fields)...
    </form>
    


    *FYI those of you who cannot get the Formit2db or db2FormIt working properly, it might be a typo in case-sensitive hook name. I was struggling on db2FormIt because I did not capitalize the i. Where as Formit2db does not capitalize the i. Hope this saves someone an hour smiley
    • Is it possible to save my informations from the FormIt input Fields to multiple tables?
      For example there is my main table called 'songs' and i want to store additional informations to a table 'places'.

      When i read out the data i want to join this informations to show it all together.

      Is there a way to do save it like this in one step?
      • That is not possible with Formit2db out of the box. You have to take the code from the hooks and modify it to your needs. [ed. note: Jako last edited this post 5 years, 2 months ago.]
        • It should be possible, with a customized save-method in your xpdo-class
            -------------------------------

            you can buy me a beer, if you like MIGX

            http://webcmsolutions.de/migx.html

            Thanks!
          • I cannot get this to work, I'm using the formit2db & db2formit snippets from the first post.

            After submit, no data is going to the table and I'm getting the following errors in Modx error log:

            [2015-08-06 19:07:54] (ERROR @ /index.php) [FormIt] Could not find hook "".
            [2015-08-06 19:07:54] (ERROR @ /index.php) Could not load class: ModxCustomTransport4u from mysql.modxcustomtransport4u.
            [2015-08-06 19:07:54] (ERROR @ /index.php) ModxCustomTransport4u::load() is not a valid static method.
            [2015-08-06 19:07:54] (ERROR @ /index.php) Could not load class: ModxCustomTransport4u from mysql.modxcustomtransport4u.


            This is the data I created the mysql table with:

            SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
            CREATE TABLE IF NOT EXISTS `modx_custom_transport4u` (
            `id` int(25) unsigned NOT NULL auto_increment,
            `dname` varchar(225) NOT NULL default '',
            `demail` varchar(20) NOT NULL default '',
            `province` varchar(200) NOT NULL default '',
            `country` varchar(200) NOT NULL default '',
            `package` varchar(200) NOT NULL default '',
            `method` varchar(200) NOT NULL default '',
            `name4` varchar(200) NOT NULL default '',
            `name2` varchar(200) NOT NULL default '',
            `name1` varchar(200) NOT NULL default '',
            `name3` varchar(200) NOT NULL default '',
            `comment` LONGTEXT NOT NULL default '',
            PRIMARY KEY (`id`)
            ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=0 ;


            This is my formit call:

            [[!FormIt?
               &hooks=`Formit2db,email`
               &preHooks=`[[!profile]],db2Formit`
               &prefix=`modx_custom_`
               &packageName=`transport4u`
               &tablename=`modx_custom_transport4u`
               &emailFrom=`mail@website.com`
               &emailTpl=`myEmailChunk`
               &emailTo=`[[+demail]]`
               &emailCC=`enquiry@website.com`
               &emailSubject=`Your Delivery Request Quote`
               &validate=`dname:required,
                  demail:required,
                  workmail:blank,
                  province:required,
                  datepicker:required,
                  country:required,
                  package:required,
                  method:required,
                  name4:required,
                  name2:required,
                  name1:required,
                  name3:required,
                  comment:required:stripTags`


            This is the schema that is created in folders inside /core/components/ upon submit:

            <?xml version="1.0" encoding="UTF-8"?>
            <model package="transport4u" baseClass="xPDOObject" platform="mysql" defaultEngine="MyISAM" version="1.1">
            	<object class="Transport4u" table="transport4u" extends="xPDOSimpleObject">
            		<field key="dname" dbtype="varchar" precision="225" phptype="string" null="false" default="" />
            		<field key="demail" dbtype="varchar" precision="20" phptype="string" null="false" default="" />
            		<field key="province" dbtype="varchar" precision="200" phptype="string" null="false" default="" />
            		<field key="country" dbtype="varchar" precision="200" phptype="string" null="false" default="" />
            		<field key="package" dbtype="varchar" precision="200" phptype="string" null="false" default="" />
            		<field key="method" dbtype="varchar" precision="200" phptype="string" null="false" default="" />
            		<field key="name4" dbtype="varchar" precision="200" phptype="string" null="false" default="" />
            		<field key="name2" dbtype="varchar" precision="200" phptype="string" null="false" default="" />
            		<field key="name1" dbtype="varchar" precision="200" phptype="string" null="false" default="" />
            		<field key="name3" dbtype="varchar" precision="200" phptype="string" null="false" default="" />
            		<field key="comment" dbtype="longtext" phptype="string" null="false" />
            	</object>
            </model>


            There is a difference between the class name generated in the schema to the 'class' in the Modx error log.

            What do I have to change or adjust to make this work so the data saves to the mysql database?

            • I got the above working and it is now saving to the database.
              What I did was remove the prefix from the tablename and the now I can see the data in the database smiley

              I am still getting this in Modx error log though:

              [2015-08-07 19:08:32] (ERROR @ /index.php) [FormIt] Could not find hook "".
              [2015-08-07 19:09:22] (ERROR @ /index.php) [FormIt] Could not find hook "".


              I am using a 'profile' prehook to auto fill some fields in the formit form and this is causing this issue (if I remove it then there is no errors).
              I can see that the fields are loading ok when I use it so I got to look at the profile fields (later because this is small issue).
              • If the hook name is 'profile' you should call it like this:
                &preHooks=`profile,db2Formit`
                • Hello

                  I'd like to use formit2db & db2formit with non-prefixed table.
                  The snippet works fine with default prefixed tables ("modx_xxxxx") and with custom prefix (
                  "custom_xxxx", &prefix="custom_"
                  )

                  But when I try to work with tables without prefix (xxxxxx), there is no output.
                  Even with param
                  &prefix=``

                  and
                     tablePrefix=""

                  in xml schema (cf. https://rtfm.modx.com/xpdo/2.x/getting-started/creating-a-model-with-xpdo/defining-a-schema/defining-the-database-and-tables)

                  Is there a way to force the snippet to work with table without prefix? [ed. note: johnxx last edited this post 4 years, 6 months ago.]
                    MODX lover
                    -
                    Développeur MODX / Webdesign / Solutions web
                  • If the prefix is empty, the package version uses the default MODX prefix.

                    https://github.com/Jako/FormIt2db/blob/master/core/components/formit2db/elements/snippets/formit2db.snippet.php#L15

                    This can't be changed in the package without BC. So duplicate both hooks and change the duplicated code to your needs.
                    • Thank you for your reply.

                      I tried to change the snippets:

                      1. $prefix = $modx->getOption('prefix', $scriptProperties, ""), true);
                      2. $prefix = $modx->getOption('prefix', $scriptProperties, null), true);
                      3. $prefix=""
                      4. $prefix=null;
                      


                      but result is same.
                      With these values XML schema doesn't include the table it supposed to add.


                      Maybe MODX absolutelly need the $prefix variable to work with XPDO (?).
                        MODX lover
                        -
                        Développeur MODX / Webdesign / Solutions web