Basic PHP Form

The most basic PHP snippet for a JsonFormBuilder form. This is pretty much identical to the Basic JSON Form, but written as PHP instead of the shorthand JSON object.

The Code

Create a snippet (any name, doesn't matter, lets assume this snippet is called JsonFormBuilder_myForm) and add the following content.

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','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(3);
//true is default, however you can set this to false to stop a number of the spam protection methods. If set to true, Javascript is required to view and submit the form.
$o_form->setSpamProtection(true); 
$o_form->addRules($a_formRules);

//SETUP EMAIL
//Note, this is not required, you may want to not send an email and record the data to a database.
$o_form->setEmailToAddress($modx->getOption('emailsender'));

$o_form->setEmailReplyToAddress($o_form->postVal('email_address'));
$o_form->setEmailReplyToName($o_form->postVal('name_full'));
$o_form->setEmailFromAddress('noreply@'.$_SERVER['HTTP_HOST']);
$o_form->setEmailFromName($this->modx->getOption('site_name'));

$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>');

//Set jQuery validation on and to be output
$o_form->setJqueryValidation(true);
//You can specify that the javascript is sent into a placeholder for those that have jquery scripts just before body close. If jquery scripts are in the head, no need for this.
$o_form->setPlaceholderJavascript('JsonFormBuilder_myForm');
  
//ADD ELEMENTS TO THE FORM IN PREFERRED ORDER
$o_form->addElements(
    array(
        $o_fe_name,$o_fe_email,$o_fe_notes,$o_fe_buttSubmit
    )
);

//The form HTML will now be available via which can be returned like so.
//NOTE: You can place this form code into a site class or whatever you like. You may do what you like with the form output.
//In the case of a successful form submission, the user will be 
return $o_form->output();

The Form

Check out how this form will render in the Base Form example.

Note: we have disabled redirect and the to email address, so submitting this form will not send an email.

The Email

The email is automatically built from the snippet (below) just like the form.