//Same setup for a radio group
$r = new FormRule(FormRuleType::required, $o_fe_catname, NULL, 'As you have a cat, please tell us its name.');
$r->setCondition(array($o_fe_havecat, 'Yes'));
$a_formRules[] = $r;
$r = new FormRule(FormRuleType::conditionShow, $o_fe_catname);
$r->setCondition(array($o_fe_havecat, 'Yes'));
$a_formRules[] = $r;
$o_fe_buttSubmit = new JsonFormBuilder_elementButton('submit', 'Submit Form', 'submit');
//Set required fields
$a_formFields_required = array($o_fe_havedog, $o_fe_havecat, $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);
$o_form->addRules($a_formRules);
$o_form->setEmailToAddress($modx->getOption('emailsender'));
$o_form->setEmailFromAddress($o_form->postVal('email_address'));
$o_form->setEmailFromName($o_form->postVal('name_full'));
$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);
$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_havedog, $o_fe_dogname, $o_fe_havecat, $o_fe_catname, $o_fe_buttSubmit));
//The form HTML will now be available via
//$o_form->output();
//This can be returned in a snippet or passed to any other script to handle in any way.
    if (empty($val) === false) {
        $phoneVal = $val;
        $foundMatches = preg_match('/^\\(?(\\d{' . $var[0] . '})\\)?[- ]?(\\d{' . $var[1] . '})[- ]?(\\d{' . $var[2] . '})$/', $phoneVal);
        if ($foundMatches !== 1) {
            return false;
        }
    }
    return true;
};
$rule->setCustomRuleValidateFunction($func);
//Finally, add the rule to the form
$a_formRules[] = $rule;
//CREATE FORM AND SETUP
$o_form = new JsonFormBuilder($modx, 'contactForm');
$o_form->setRedirectDocument(3);
$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->setEmailFromAddress('*****@*****.**');
$o_form->setEmailFromName('No One');
$o_form->setEmailSubject('JsonFormBuilder Custom Validate Test');
//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_phone, $o_fe_buttSubmit));
//The form HTML will now be available via
//$o_form->output();
//This can be returned in a snippet or passed to any other script to handle in any way.