Upgrading from FormItBuilder

In most cases upgrading to JsonFormBuilder is simple. 95% of style classes, structure and calls are identical. Will a few very short steps you can start using JsonFormBuilder on projects with FormItBuilder installed.

Debugging where your code is failing is critical. Regardless of the local PHP settings, placing this line at the top of your script temporarily may help to quickly get a better idea on where your form is failing.
ini_set('display_errors', '1');

  1. Replace all references to FormItBuilder in your snippet with JsonFormBuilder. A simple find and replace will do this in a second.
  2. You can no longer use MODX placeholders ( like [[+name]] ) any more. These need to be replaced with calls such as $o_form->postVal('name') or system settings like $modx->getOption('emailsender').
  3. There is no need to pass snippet names or anyhting into a custom build function. This is not needed. Remove all of this and replace with a single require require_once $modx->getOption('core_path',null,MODX_CORE_PATH).'components/jsonformbuilder/model/jsonformbuilder/JsonFormBuilder.class.php';
  4. Simply return the form after it is created and remove the if/else condition at the bottom of your old FormItBuilder. There is no need to have if (isset($outputType) === false) ... any more, simply return $o_form->output(); and your done.
  5. There is no JsonFormBuilder_htmlBlock any more, you simply add the string into the array.
  6. Some auto responder method names changed for consistency. Instead of setAutoResponderToAddressField it is now setAutoResponderToAddress (along with a few other small changes). Take a look at the Auto Responders example if you are upgrading a script with auto responders.
  7. There are no setHooks or setPostHookName methods any more. To do custom interaction with forms simply write it direct into the snippet with php similar to that shown in Custom Validation where the line echo 'Form was submitted and was valid.'; shown.

Change this (FormItBuilder Form)

<?php
$snippetName='FormItBuilder_BasicExample';
require_once $modx->getOption('core_path',null,MODX_CORE_PATH).'components/formitbuilder/model/formitbuilder/FormItBuilder.class.php';
if (function_exists('FormItBuilder_BasicExample')===false) {
function FormItBuilder_BasicExample(modX &$modx, $snippetName) {
  
      
      
      
//CREATE FORM ELEMENTS
$o_fe_name      = new FormItBuilder_elementText('name_full','Your Name');
$o_fe_email     = new FormItBuilder_elementText('email_address','Email Address');
$o_fe_notes     = new FormItBuilder_elementTextArea('comments','Comments',5,30);
$o_fe_buttSubmit    = new FormItBuilder_elementButton('submit','Submit Form','submit');
  
//SET VALIDATION RULES
$a_formRules=array();
//Set required fields
$a_formFields_required = array($o_fe_notes, $o_fe_name, $o_fe_email);
foreach($a_formFields_required as $field){
    $a_formRules[] = new FormRule(FormRuleType::required,$field);
}
//make email field require a valid email address
$a_formRules[] = new FormRule(FormRuleType::email, $o_fe_email, NULL, 'Please provide a valid email address');
  
//CREATE FORM AND SETUP
$o_form = new FormItBuilder($modx,'contactForm');
$o_form->setHooks(array('spam','email','redirect'));
$o_form->setRedirectDocument(5);
$o_form->addRules($a_formRules);
$o_form->setPostHookName($snippetName);
$o_form->setEmailToAddress('your@email.address');
$o_form->setEmailFromAddress('[[+email_address]]');
$o_form->setEmailSubject('FormItBuilder Contact Form Submission - From: [[+name_full]]');
$o_form->setEmailHeadHtml('<p>This is a response sent by [[+name_full]] using the contact us form:</p>');
$o_form->setJqueryValidation(true);
  
//ADD ELEMENTS TO THE FORM IN PREFERRED ORDER
$o_form->addElements(
    array(
        $o_fe_name,$o_fe_email,$o_fe_notes,
        new FormItBuilder_htmlBlock('<hr class="formSpltter" />'),
        $o_fe_buttSubmit
    )
);
  
return $o_form;
      
  
  
  
}
}
//Run the form construction function above
$o_form = FormItBuilder_BasicExample($modx, $snippetName);
if (isset($outputType) === false) {
    //this same snippet was called via various other hooks
    return $o_form->processCoreHook($hook, $o_form);
} else {
    //Final output for form
    return $o_form->output();
}
?>

To this (JsonFormBuilder form)

<?php
require_once $modx->getOption('core_path',null,MODX_CORE_PATH).'components/jsonformbuilder/model/jsonformbuilder/JsonFormBuilder.class.php';

//CREATE FORM ELEMENTS
$o_fe_name      = new JsonFormBuilder_elementText('name_full','Your Name');
$o_fe_email     = new JsonFormBuilder_elementText('email_address','Email Address');
$o_fe_notes     = new JsonFormBuilder_elementTextArea('comments','Comments',5,30);
$o_fe_buttSubmit    = new JsonFormBuilder_elementButton('submit','Submit Form','submit');
  
//SET VALIDATION RULES
$a_formRules=array();
//Set required fields
$a_formFields_required = array($o_fe_notes, $o_fe_name, $o_fe_email);
foreach($a_formFields_required as $field){
    $a_formRules[] = new FormRule(FormRuleType::required,$field);
}
//make email field require a valid email address
$a_formRules[] = new FormRule(FormRuleType::email, $o_fe_email, NULL, 'Please provide a valid email address');
  
//CREATE FORM AND SETUP
$o_form = new JsonFormBuilder($modx,'contactForm');
$o_form->setRedirectDocument(5);
$o_form->addRules($a_formRules);
$o_form->setEmailToAddress('your@email.address');
$o_form->setEmailFromAddress($o_form->postVal('email_address'));
$o_form->setEmailSubject('JsonFormBuilder Contact Form Submission - From: '.$o_form->postVal('name_full'));
$o_form->setEmailHeadHtml('<p>This is a response sent by '.$o_form->postVal('name_full').' using the contact us form:</p>');
$o_form->setJqueryValidation(true);
  
//ADD ELEMENTS TO THE FORM IN PREFERRED ORDER
$o_form->addElements(
    array(
        $o_fe_name,$o_fe_email,$o_fe_notes,
        '<hr class="formSpltter" />',
        $o_fe_buttSubmit
    )
);
  
return $o_form->output();
?>