$modx->runSnippet('Formit') does not execute

Hey all,

I am stumped with this issue. For whatever reason $modx->runSnippet(‘Formit’) will not run.

I have a snippet called test and here in my code

<?php
$modx->runSnippet('FormIt', array(
    'prehooks'=>'loadCustomValues',
    'hooks'=>'email,FormItSaveForm',
    'emailTpl'=>'MyEmailChunk',
    'emailTo'=>'user@example.com',
    'emailSubject'=>'test email',
    'formName' => 'test form',
));

$modx->runSnippet('QuickEmail', array(
    'to'=>'user@example.com',
));

The QuickEmail runs but not the FormIt. My error logs are not showing my anything, I cleared my all my cache, FormIt will in an empty template but not via a snippet.

Any suggestions?

FormIt’s job is to process user input from a submitted form on the screen. Since there’s no form, nothing has been submitted, so I’m not sure it can be used with runSnippet().

It’s possible that if you put the appropriate data into the $_POST array and then called FormIt with runSnippet() FormIt might process that data as a submitted form, though I’m not sure why you would want to do that, since it would be much simpler to just send the email with QuickEmail or call PhpMailer or the equivalent in your code.

If you want user input for FormIt to respond to, you’ll have to create a form to gather that and put it on the page. In that case, you might as well put the FormIt tag on that page.

1 Like

ahhhh ok…very noob mistake on my end. I will use PhpMailer. Thanks alot Bob.

Just check out https://docs.modx.com/current/en/extending-modx/services/modmail before jumping in with PHP mailer. The ModMail service makes life so much easier

I know this is an ancient thread and you have probably long forgotten about this. But for reference and others coming across this thread, I thought I’d share some new insights.

Recently I’ve had to use Formit to send a user off-site then reprocess the data and send an email when the user is sent back to the site. I decided to use Formit Hooks with a twist.

To do this I added a new &successHooks parameter to the Formit snippet that holds the email hook. When the user is redirected back to the page, Formit fires a prehook which in turn recalls the previously submitted data & data returned from the 3rd party site, then assigns it to the $_POST array then calls the $modx->runSnippet();. This works great, however, you must remember to unset the $_POST array i.e. $_POST = [] otherwise the main Formit snippet will rePost the form.

// This is an example of the preHook 

// Set properties 
$scriptProperties = $hook->formit->config;
// Reassign and unset 
unset($scriptProperties['successHooks']);
unset($scriptProperties['preHooks']);
unset($scriptProperties['submitVar']);
unset($scriptProperties['validate']);
// Reassign successHooks to Hooks param
$scriptProperties['hooks'] = $hook->formit->config['successHooks'];

$_POST = $_SESSION['formit']['values'];
// Now re-run formit with the post hooks 
$modx->runSnippet('formit', $scriptProperties);
// Clear out the POST Array 
$_POST = [];

I’ll add onto this, in case someone isn’t already in a FormIt context - backstory, I had to save a $_POST submission to FormItSaveForm. Using QuickApi, a $_POST is sent to an endpoint. The endpoint represents a snippet that processes everything, but when I runSnippet FormIt, the $_POST wasn’t consumed like it should’ve been.

Since runSnippet wasn’t working, I tried by just loading the class like in the FormIt snippet:

/* Generate $formFields and $fieldNames from $_POST */
$scriptProperties = [
    'hooks' => 'FormItSaveForm',
    'formName' => $formName,
    'formFields' => implode(',', $formFields),
    'fieldNames' => implode(',', $fieldNames),
];

/*From the FormIt snippet */
$modelPath = $modx->getOption(
    'formit.core_path',
    null,
    $modx->getOption('core_path', null, MODX_CORE_PATH) . 'components/formit/'
) . 'model/formit/';
$modx->loadClass('FormIt', $modelPath, true, true);
$fi = new FormIt($modx, $scriptProperties);

try {
    $fi->initialize($modx->context->get('key'));
    $fi->loadRequest();
} catch(Exception $err) {
    $fa->log($formID.' Error Loading FormIt Request');
}

$fields = $fi->request->prepare();
return $fi->request->handle($fields);

I am willing to be this is not the best solution, but by creating the class inside of the snippet like that, it was able to access the $_POST again.

Interesting. What does your endpoint look like? Is it in /assets/components/mypackage/endpoint.php or a snippet that is called from a Modx resource i.e. /endpoint/ → [[!myEndpointSnippet]]?

The extra I’m using, QuickApi does have a resource with a snippet on it.

Then it uses a plugin to reroute requests that go to your specified base route, default is api, to that resource. The snippet then loads the QuickApi class where it does the rest of its :star2:magic​:star2:.