public function setType($type)
 {
     if ($type != 'select' && $type != 'text' && $type != 'radio' && $type != 'check') {
         JsonFormBuilder::throwError('[Element: ' . $this->_id . '] Not a valid type, must be "select", "text", "radio" or "check".');
     } else {
         $this->s_type = $type;
     }
 }
 /**
  * showIndividualLabels($value)
  * 
  * By default (true) each radio option will have its own label. Users may wish to have radio options in some kind of table with custom surrounding HTML. In this case labels can be hidden.  If no value passed the method will return the current label display status. 
  * @param boolean $value
  * @return boolean 
  */
 public function showIndividualLabels($value)
 {
     if (func_num_args() == 0) {
         return $this->_showIndividualLabels;
     } else {
         $this->_showIndividualLabels = JsonFormBuilder::forceBool(func_get_arg(0));
     }
 }
 function getJsonVal($arr, $key, $required = false)
 {
     if (isset($arr[$key]) && $arr[$key] != '') {
         return $arr[$key];
     } else {
         if ($required) {
             JsonFormBuilder::throwError('Failed to find required JSON property "' . $key . '".');
         }
     }
     return false;
 }
 /**
  * outputHTML()
  * 
  * Outputs the HTML for the element.
  * @return string 
  */
 public function outputHTML()
 {
     $s_ret = '<input id="' . htmlspecialchars($this->_id) . '" type="' . htmlspecialchars($this->_type) . '" value="' . htmlspecialchars($this->_label) . '"';
     if ($this->_type == 'image') {
         if ($this->_src === NULL) {
             JsonFormBuilder::throwError('[Element: ' . $this->_id . '] Button of type "image" must have a src set.');
         } else {
             $s_ret .= ' src="' . htmlspecialchars($this->_src) . '"';
         }
     }
     $s_ret .= ' ' . $this->processExtraAttribsToStr() . ' />';
     return $s_ret;
 }
 /**
  * setMaxLength($value)
  * 
  * Sets the maximum number of checkboxes that can be selected before the checkbox group will be valid (e.g. please select up to three options...).
  * @param int $value 
  */
 public function setMaxLength($value)
 {
     $value = JsonFormBuilder::forceNumber($value);
     if ($this->_minLength !== NULL && $this->_minLength > $value) {
         throw JsonFormBuilder::throwError('[Element: ' . $this->_id . '] Cannot set maximum length to "' . $value . '" when minimum length is "' . $this->_minLength . '"');
     } else {
         $this->_maxLength = JsonFormBuilder::forceNumber($value);
     }
 }
 public function setMaxFilesize($value)
 {
     $value = (int) round($value);
     $this->_maxFilesizeBytes = JsonFormBuilder::forceNumber($value);
 }
//$val will contain the form element value, $var will contain the contents of the param specified in step 3 above.
$func = function ($val = null, $var = null) {
    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
 public function refresh()
 {
     $type = $this->getType();
     $validationMessage = $this->getValidationMessage();
     $element = $this->getElement();
     $value = $this->getValue();
     if (empty($type) || empty($element)) {
         //if elements have not yet passed, don't bother setting validation messages.
         return;
     }
     switch ($type) {
         //form field match, password confirm etc
         case FormRuleType::fieldMatch:
             if ($value && is_object($value)) {
                 if ($validationMessage === NULL) {
                     $this->_validationMessage = $element->getLabel() . ' must match ' . $value->getLabel();
                 }
             } else {
                 $this->_validationMessage = $element->getLabel() . ' does not match';
             }
             break;
             //true false type validators
         //true false type validators
         case FormRuleType::email:
             if ($validationMessage === NULL) {
                 $this->_validationMessage = $element->getLabel() . ' must be a valid email address';
             }
             break;
             //true false type validators
         //true false type validators
         case FormRuleType::url:
             if ($validationMessage === NULL) {
                 $this->_validationMessage = $element->getLabel() . ' must be a valid URL';
             }
             break;
         case FormRuleType::numeric:
             if ($validationMessage === NULL) {
                 $this->_validationMessage = $element->getLabel() . ' must be numeric';
             }
             break;
         case FormRuleType::required:
             if ($validationMessage === NULL) {
                 $this->_validationMessage = $element->getLabel() . ' es requerido';
             }
             $element->isRequired(true);
             break;
             //value driven number type validators
         //value driven number type validators
         case FormRuleType::maximumLength:
             if ($value) {
                 $value = JsonFormBuilder::forceNumber($value);
                 if ($validationMessage === NULL) {
                     if (is_a($element, 'JsonFormBuilder_elementCheckboxGroup')) {
                         $this->_validationMessage = $element->getLabel() . ' must have less than ' . ($value + 1) . ' selected';
                     } else {
                         $this->_validationMessage = $element->getLabel() . ' can only contain up to ' . $value . ' characters';
                     }
                 }
                 $element->setMaxLength($value);
             }
             break;
         case FormRuleType::maximumValue:
             if ($value) {
                 $value = JsonFormBuilder::forceNumber($value);
                 if ($validationMessage === NULL) {
                     $this->_validationMessage = $element->getLabel() . ' must not be greater than ' . $value;
                 }
                 $element->setMaxValue($value);
             }
             break;
         case FormRuleType::minimumLength:
             if ($value) {
                 $value = JsonFormBuilder::forceNumber($value);
                 if ($validationMessage === NULL) {
                     if (is_a($element, 'JsonFormBuilder_elementCheckboxGroup')) {
                         $this->_validationMessage = $element->getLabel() . ' must have at least ' . $value . ' selected';
                     } else {
                         $this->_validationMessage = $element->getLabel() . ' must be at least ' . $value . ' characters';
                     }
                 }
                 $element->setMinLength($value);
             }
             break;
         case FormRuleType::minimumValue:
             if ($value) {
                 $value = JsonFormBuilder::forceNumber($value);
                 if ($validationMessage === NULL) {
                     $this->_validationMessage = $element->getLabel() . ' must not be less than ' . $value;
                 }
                 $element->setMinValue($value);
             }
             break;
         case FormRuleType::date:
             /*
              Supports any single character separator with any order of dd,mm and yyyy
              Example: yyyy-dd-mm dd$$mm$yyyy dd/yyyy/mm.
              Dates will be split and check if a real date is entered.
             */
             if ($value) {
                 $value = strtolower(trim($value));
                 if (empty($value) === true) {
                     JsonFormBuilder::throwError('Date type field must have a value (date format) specified.');
                 }
                 if ($validationMessage === NULL) {
                     $this->_validationMessage = $element->getLabel() . ' must be a valid date (===dateformat===).';
                 }
             }
             break;
         case FormRuleType::file:
             if ($validationMessage === NULL) {
                 $this->_validationMessage = $element->getLabel() . ' must be a valid file.';
             }
             break;
         case FormRuleType::conditionShow:
             //is only used by jQuery
             break;
         case FormRuleType::custom:
             if ($validationMessage === NULL) {
                 $this->_validationMessage = $element->getLabel() . ' is not valid.';
             }
             break;
         default:
             JsonFormBuilder::throwError('Type "' . $type . '" not valid. Recommend using FormRule constant');
             break;
     }
 }
 public function validate()
 {
     //prepare can be called multiple times for simplicity, but should only run once.
     if ($this->b_validated === true) {
         return;
     } else {
         $this->b_validated = true;
     }
     //if security field has been filled, kill script with a false thankyou.
     $secVar = $this->postVal($this->_id . '_fke' . date('Y') . 'Sp' . date('m') . 'Blk');
     //This vields value is set with javascript. If the field does not equal the secredvalue
     $secVar2 = $this->postVal($this->_id . '_fke' . date('Y') . 'Sp' . date('m') . 'Blk2');
     if (strlen($secVar) > 0) {
         $this->spamDetectExit(1);
     }
     if ($secVar2 !== false && $secVar2 != '1962') {
         $this->spamDetectExit(2);
     }
     $this->setIsSubmitted(false);
     $s_submittedVal = $this->postVal('submitVar_' . $this->_id);
     if (empty($s_submittedVal) === false) {
         $this->setIsSubmitted(true);
     }
     //process and add form rules
     $a_fieldProps_jqValidate = array();
     $a_fieldProps_jqValidateGroups = array();
     $a_fieldProps_errstringJq = array();
     $a_footJavascript = array();
     //Keep tally of all validation errors. If posted and 0, form will continue.
     foreach ($this->_rules as $rule) {
         $o_elFull = $rule->getElement();
         //verify this element is actually in the form
         $b_found = false;
         foreach ($this->_formElements as $o_el) {
             if ($o_elFull === $o_el) {
                 $b_found = true;
                 break;
             }
         }
         if ($b_found === false) {
             JsonFormBuilder::throwError('Rule "' . $rule->getType() . '" for element "' . $o_elFull->getId() . '" specified, but element is not in form.');
         }
         if (is_array($o_elFull) === true) {
             $o_el = $o_elFull[0];
         } else {
             $o_el = $o_elFull;
         }
         $elId = $o_el->getId();
         //used to test simple single post values
         $s_postedValue = $this->postVal($o_el->getId());
         $elName = $o_el->getName();
         if (isset($a_fieldProps_jqValidate[$elId]) === false) {
             $a_fieldProps_jqValidate[$elId] = array();
         }
         if (isset($a_fieldProps_errstringJq[$elId]) === false) {
             $a_fieldProps_errstringJq[$elId] = array();
         }
         $s_validationMessage = $rule->getValidationMessage();
         switch ($rule->getType()) {
             case FormRuleType::email:
                 $a_fieldProps_jqValidate[$elName][] = 'email:true';
                 $a_fieldProps_errstringJq[$elName][] = 'email:' . json_encode($s_validationMessage);
                 if (!empty($s_postedValue)) {
                     if (filter_var($s_postedValue, FILTER_VALIDATE_EMAIL) === false) {
                         $this->_invalidElements[] = $o_el;
                         $o_el->errorMessages[] = $s_validationMessage;
                     }
                 }
                 break;
             case FormRuleType::url:
                 $a_fieldProps_jqValidate[$elName][] = 'url:true';
                 $a_fieldProps_errstringJq[$elName][] = 'url:' . json_encode($s_validationMessage);
                 if (!empty($s_postedValue)) {
                     if (filter_var($s_postedValue, FILTER_VALIDATE_URL) === false) {
                         $this->_invalidElements[] = $o_el;
                         $o_el->errorMessages[] = $s_validationMessage;
                     }
                 }
                 break;
             case FormRuleType::fieldMatch:
                 $val = $rule->getValue();
                 if (is_a($val, 'JsonFormBuilder_baseElement') === false) {
                     $val = $this->getElementById($val);
                 }
                 if (is_a($val, 'JsonFormBuilder_baseElement') === false) {
                     JsonFormBuilder::throwError('Element for fieldMatch not found for "' . htmlspecialchars($elName) . '" rule. Specify a valid ID or Element Object');
                 }
                 $a_fieldProps_jqValidate[$elName][] = 'equalTo:"#' . $val->getId() . '"';
                 $a_fieldProps_errstringJq[$elName][] = 'equalTo:' . json_encode($s_validationMessage);
                 //validation check
                 $val1 = $this->postVal($o_elFull->getId());
                 $val2 = $this->postVal($val->getId());
                 if ($val1 !== $val2) {
                     $this->_invalidElements[] = $o_el;
                     $o_el->errorMessages[] = $s_validationMessage;
                 }
                 break;
             case FormRuleType::maximumLength:
                 $val = (int) $rule->getValue();
                 $a_fieldProps_jqValidate[$elName][] = 'maxlength:' . $val;
                 $a_fieldProps_errstringJq[$elName][] = 'maxlength:' . json_encode($s_validationMessage);
                 if (is_a($o_el, 'JsonFormBuilder_elementCheckboxGroup')) {
                     //validation check
                     $a_elementsSelected = $this->postVal($o_el->getId());
                     if (is_array($a_elementsSelected) === true && count($a_elementsSelected) > 0) {
                         //ignore if not selected at all as "required" should pick this up.
                         if (is_array($a_elementsSelected) === false || count($a_elementsSelected) > $val) {
                             $this->_invalidElements[] = $o_el;
                             $o_el->errorMessages[] = $s_validationMessage;
                         }
                     }
                 } else {
                     //validation check
                     if (strlen($s_postedValue) > $val) {
                         $this->_invalidElements[] = $o_el;
                         $o_el->errorMessages[] = $s_validationMessage;
                     }
                 }
                 break;
             case FormRuleType::maximumValue:
                 $val = (int) $rule->getValue();
                 $a_fieldProps_jqValidate[$elName][] = 'max:' . $val;
                 $a_fieldProps_errstringJq[$elName][] = 'max:' . json_encode($s_validationMessage);
                 //validation check
                 if ($s_postedValue != '' && (int) $s_postedValue > $val) {
                     $this->_invalidElements[] = $o_el;
                     $o_el->errorMessages[] = $s_validationMessage;
                 }
                 break;
             case FormRuleType::minimumLength:
                 $val = (int) $rule->getValue();
                 $a_fieldProps_jqValidate[$elName][] = 'minlength:' . $val;
                 $a_fieldProps_errstringJq[$elName][] = 'minlength:' . json_encode($s_validationMessage);
                 if (is_a($o_el, 'JsonFormBuilder_elementCheckboxGroup')) {
                     //validation check
                     $a_elementsSelected = $this->postVal($o_el->getId());
                     if (is_array($a_elementsSelected) === true && count($a_elementsSelected) > 0) {
                         //ignore if not selected at all as "required" should pick this up.
                         if (is_array($a_elementsSelected) === false || count($a_elementsSelected) < $val) {
                             $this->_invalidElements[] = $o_el;
                             $o_el->errorMessages[] = $s_validationMessage;
                         }
                     }
                 } else {
                     //validation check
                     if (strlen($s_postedValue) < $val) {
                         $this->_invalidElements[] = $o_el;
                         $o_el->errorMessages[] = $s_validationMessage;
                     }
                 }
                 break;
             case FormRuleType::minimumValue:
                 $val = (int) $rule->getValue();
                 $a_fieldProps_jqValidate[$elName][] = 'min:' . $val;
                 $a_fieldProps_errstringJq[$elName][] = 'min:' . json_encode($s_validationMessage);
                 //validation check
                 if ($s_postedValue != '' && (int) $s_postedValue < $val) {
                     $this->_invalidElements[] = $o_el;
                     $o_el->errorMessages[] = $s_validationMessage;
                 }
                 break;
             case FormRuleType::numeric:
                 $a_fieldProps_jqValidate[$elName][] = 'digits:true';
                 $a_fieldProps_errstringJq[$elName][] = 'digits:' . json_encode($s_validationMessage);
                 //validation check
                 if ($s_postedValue != '' && ctype_digit($s_postedValue) === false) {
                     $this->_invalidElements[] = $o_el;
                     $o_el->errorMessages[] = $s_validationMessage;
                 }
                 break;
             case FormRuleType::required:
                 $jqRequiredVal = 'true';
                 $ruleCondition = $rule->getCondition();
                 if (!empty($ruleCondition) && is_a($ruleCondition[0], 'JsonFormBuilder_baseElement') === false) {
                     $ruleCondition[0] = $this->getElementById($ruleCondition[0]);
                 }
                 $b_validateRequiredPost = true;
                 if (!empty($ruleCondition)) {
                     $this_elID = $ruleCondition[0]->getId();
                     if (is_a($ruleCondition[0], 'JsonFormBuilder_elementRadioGroup')) {
                         $s_valJq = 'jQuery("input[type=radio][name=' . $this_elID . ']")';
                     } else {
                         $s_valJq = 'jQuery("#' . $this_elID . '")';
                     }
                     $jqRequiredVal = '{depends:function(element){var v=' . $s_valJq . '.val(); return (v=="' . rawurlencode($ruleCondition[1]) . '"?true:false); }}';
                     $b_validateRequiredPost = false;
                     if ($this->postVal($this_elID) == $ruleCondition[1]) {
                         $b_validateRequiredPost = true;
                     }
                 }
                 if (is_a($o_el, 'JsonFormBuilder_elementMatrix')) {
                     $s_type = $o_el->getType();
                     $a_rows = $o_el->getRows();
                     $a_columns = $o_el->getColumns();
                     $a_namesForGroup = array();
                     switch ($s_type) {
                         case 'text':
                             for ($row_cnt = 0; $row_cnt < count($a_rows); $row_cnt++) {
                                 for ($col_cnt = 0; $col_cnt < count($a_columns); $col_cnt++) {
                                     $a_namesForGroup[] = $elName . '_' . $row_cnt . '_' . $col_cnt;
                                     $a_fieldProps_jqValidate[$elName . '_' . $row_cnt . '_' . $col_cnt][] = 'required:' . $jqRequiredVal;
                                     $a_fieldProps_errstringJq[$elName . '_' . $row_cnt . '_' . $col_cnt][] = 'required:' . json_encode($s_validationMessage);
                                 }
                             }
                             break;
                         case 'radio':
                             for ($row_cnt = 0; $row_cnt < count($a_rows); $row_cnt++) {
                                 $a_namesForGroup[] = $elName . '_' . $row_cnt;
                                 $a_fieldProps_jqValidate[$elName . '_' . $row_cnt][] = 'required:' . $jqRequiredVal;
                                 $a_fieldProps_errstringJq[$elName . '_' . $row_cnt][] = 'required:' . json_encode($s_validationMessage);
                             }
                             break;
                         case 'check':
                             for ($row_cnt = 0; $row_cnt < count($a_rows); $row_cnt++) {
                                 $s_fieldName = $elName . '_' . $row_cnt . '[]';
                                 $a_namesForGroup[] = $s_fieldName;
                                 $a_fieldProps_jqValidate[$s_fieldName][] = 'required:' . $jqRequiredVal;
                                 $a_fieldProps_errstringJq[$s_fieldName][] = 'required:' . json_encode($s_validationMessage);
                             }
                             break;
                     }
                     $a_fieldProps_jqValidateGroups[$elName] = implode(' ', $a_namesForGroup);
                     //validation check
                     $b_isMatrixValid = JsonFormBuilder::is_matrix_required_valid($o_el);
                     if ($b_validateRequiredPost && $b_isMatrixValid === false) {
                         $this->_invalidElements[] = $o_el;
                         $o_el->errorMessages[] = $s_validationMessage;
                     }
                 } else {
                     if (is_a($o_el, 'JsonFormBuilder_elementCheckboxGroup')) {
                         //validation check
                         $a_elementsSelected = $this->postVal($o_el->getId());
                         if ($b_validateRequiredPost && (is_array($a_elementsSelected) === false || count($a_elementsSelected) === 0)) {
                             $this->_invalidElements[] = $o_el;
                             $o_el->errorMessages[] = $s_validationMessage;
                         }
                     } else {
                         if (is_a($o_el, 'JsonFormBuilder_elementFile')) {
                             //validation check
                             if (isset($_FILES[$o_el->getId()]) === true && $_FILES[$o_el->getId()]['size'] != 0) {
                                 //file is uploaded
                             } else {
                                 if ($b_validateRequiredPost) {
                                     $this->_invalidElements[] = $o_el;
                                     $o_el->errorMessages[] = $s_validationMessage;
                                 }
                             }
                         } else {
                             if (is_a($o_el, 'JsonFormBuilder_elementDate')) {
                                 $a_fieldProps_jqValidate[$elName . '_0'][] = 'required:' . $jqRequiredVal . ',dateElementRequired:true';
                                 $a_fieldProps_errstringJq[$elName . '_0'][] = 'required:' . json_encode($s_validationMessage) . ',dateElementRequired:' . json_encode($s_validationMessage);
                                 //validation check
                                 $elID = $o_el->getId();
                                 $postVal0 = $this->postVal($elID . '_0');
                                 $postVal1 = $this->postVal($elID . '_1');
                                 $postVal2 = $this->postVal($elID . '_2');
                                 if (empty($postVal0) === false && empty($postVal1) === false && empty($postVal2) === false) {
                                     //all three date elements must be selected
                                 } else {
                                     if ($b_validateRequiredPost) {
                                         $this->_invalidElements[] = $o_el;
                                         $o_el->errorMessages[] = $s_validationMessage;
                                     }
                                 }
                             } else {
                                 $a_fieldProps_jqValidate[$elName][] = 'required:' . $jqRequiredVal;
                                 $a_fieldProps_errstringJq[$elName][] = 'required:' . json_encode($s_validationMessage);
                                 //validation check
                                 if ($b_validateRequiredPost && strlen($s_postedValue) < 1) {
                                     $this->_invalidElements[] = $o_el;
                                     $o_el->errorMessages[] = $s_validationMessage;
                                 }
                             }
                         }
                     }
                 }
                 break;
             case FormRuleType::date:
                 $s_thisVal = $rule->getValue();
                 $s_thisErrorMsg = str_replace('===dateformat===', $s_thisVal, $s_validationMessage);
                 $a_fieldProps_jqValidate[$elName][] = 'dateFormat:\'' . $s_thisVal . '\'';
                 $a_fieldProps_errstringJq[$elName][] = 'dateFormat:' . json_encode($s_thisErrorMsg);
                 //validation check
                 $a_formatInfo = JsonFormBuilder::is_valid_date($s_postedValue, $s_thisVal);
                 if ($b_validateRequiredPost && $a_formatInfo['status'] === false) {
                     $this->_invalidElements[] = $o_el;
                     $o_el->errorMessages[] = $s_thisErrorMsg;
                 }
                 break;
             case FormRuleType::custom:
                 $custRuleName = $rule->getCustomRuleName();
                 if (empty($custRuleName)) {
                     JsonFormBuilder::throwError('CustomRuleName for custom rule not set (Element "' . htmlspecialchars($elName) . '" ' . self::fieldMatch . '").');
                 } else {
                     $custval = 'true';
                     $custRuleParam = $rule->getCustomRuleParam();
                     if (!empty($custRuleParam)) {
                         $custval = json_encode($custRuleParam);
                     }
                     $a_fieldProps_jqValidate[$elName][] = $custRuleName . ':' . $custval;
                     $a_fieldProps_errstringJq[$elName][] = $custRuleName . ':' . json_encode($s_validationMessage);
                     //validation check
                     $func = $rule->getCustomRuleValidateFunction();
                     if (!empty($func)) {
                         //validate server side
                         if (!empty($custRuleParam)) {
                             $valid = $func($s_postedValue, $custRuleParam);
                         } else {
                             $valid = $func($s_postedValue);
                         }
                         if ($valid !== true) {
                             $this->_invalidElements[] = $o_el;
                             $o_el->errorMessages[] = $s_validationMessage;
                         }
                     }
                 }
                 break;
             case FormRuleType::conditionShow:
                 $jqRequiredVal = 'true';
                 $ruleCondition = $rule->getCondition();
                 if (!empty($ruleCondition) && is_a($ruleCondition[0], 'JsonFormBuilder_baseElement') === false) {
                     $ruleCondition[0] = $this->getElementById($ruleCondition[0]);
                 }
                 $b_validateRequiredPost = true;
                 if (!empty($ruleCondition)) {
                     $this_elID = $ruleCondition[0]->getId();
                     if (count($a_footJavascript) == 0) {
                         $a_footJavascript[] = 'var a; var e; var v; var b_s; var w;';
                     }
                     //input[type=radio][name=bedStatus]
                     $a_footJavascript[] = '' . 'b_v=false;' . (is_a($ruleCondition[0], 'JsonFormBuilder_elementRadioGroup') ? 'a=jQuery("input[type=radio][name=' . $this_elID . ']"); if(a.is(":checked")===false){v="";}else{v=a.val();}' : 'a=jQuery("#' . $this_elID . '"); v=a.val();') . 'if(v=="' . rawurlencode($ruleCondition[1]) . '"){ b_v=true; }' . 'e=jQuery("#' . $o_elFull->getId() . '");' . 'w=e.parents(".formSegWrap");' . 'if(b_v){w.show();}else{ w.hide(); }' . 'a.change(function(){ var e=jQuery("#' . $o_elFull->getId() . '"); var w=e.parents(".formSegWrap"); if(jQuery(this).val()=="' . rawurlencode($ruleCondition[1]) . '"){ w.show(); }else{ w.hide(); } });' . '';
                 }
                 break;
         }
     }
     //validate on elements themselves
     foreach ($this->_formElements as $o_el) {
         if (is_object($o_el) === false) {
             //
         } else {
             $s_elClass = get_class($o_el);
             $s_postedValue = $this->postVal($o_el->getId());
             if ($s_elClass == 'JsonFormBuilder_elementFile') {
                 $this->_attachmentIncluded = true;
                 $id = $o_el->getId();
                 $a_allowedExtenstions = $o_el->getAllowedExtensions();
                 $i_maxSize = $o_el->getMaxFilesize();
                 if ($a_allowedExtenstions) {
                     $s_extInvalidMessage = 'Only ' . strtoupper(implode(', ', $a_allowedExtenstions)) . ' files allowed.';
                     $a_fieldProps_jqValidate[$id][] = 'fileExtValid:' . json_encode($a_allowedExtenstions);
                     $a_fieldProps_errstringJq[$id][] = 'fileExtValid:"' . $s_extInvalidMessage . '"';
                 }
                 if ($i_maxSize) {
                     $mbOrkb = 'mb';
                     $size = round($i_maxSize / 1024 / 1024, 2);
                     if ($size < 1) {
                         $size = round($i_maxSize / 1024);
                         $mbOrkb = 'kb';
                     }
                     $s_sizeInvalidMessage = 'File exceeds size limit (' . $size . $mbOrkb . ').';
                     $a_fieldProps_jqValidate[$id][] = 'fileSizeValid:' . $i_maxSize;
                     $a_fieldProps_errstringJq[$id][] = 'fileSizeValid:"' . $s_sizeInvalidMessage . '"';
                 }
                 //validation
                 if (isset($_FILES[$id]['size']) === true && $_FILES[$id]['size'] > 0) {
                     if ($o_el->isAllowedFilename($_FILES[$id]['name']) === false) {
                         $this->_invalidElements[] = $o_el;
                         $o_el->errorMessages[] = $s_extInvalidMessage;
                     }
                     if ($o_el->isAllowedSize($_FILES[$id]['size']) === false) {
                         $this->_invalidElements[] = $o_el;
                         $o_el->errorMessages[] = $s_sizeInvalidMessage;
                     }
                 }
             }
         }
     }
     $this->_fieldProps_jqValidate = $a_fieldProps_jqValidate;
     $this->_fieldProps_jqValidateGroups = $a_fieldProps_jqValidateGroups;
     $this->_fieldProps_errstringJq = $a_fieldProps_errstringJq;
     $this->_footJavascript = $a_footJavascript;
 }
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_buttSubmit = new JsonFormBuilder_elementButton('submit', 'Submit Form', 'submit');
//SET VALIDATION RULES
$a_formRules = array();
//Set required fields
$a_formFields_required = array($o_fe_name, $o_fe_email);
foreach ($a_formFields_required as $field) {
    $a_formRules[] = new FormRule(FormRuleType::required, $field);
}
$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->addRules($a_formRules);
$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_buttSubmit));
//must force form to run validate check prior to checking if submitted
$o_form->validate();
if ($o_form->isSubmitted() === true && count($o_form->getInvalidElements()) === 0) {
    //form was submitted and is valid. Now we can do what we want and redirect elsewhere manually.
    echo 'Form was submitted and was valid.';
    exit;
} else {
    //otherwise run the form as normal (which will display the errors).
    //$o_form->output();
}
//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(3);
$o_form->addRules($a_formRules);
//////////////////
//AUTO RESPONDER//
//////////////////
//Set email addresses and format
$o_form->setAutoResponderToAddress($o_form->postVal('email_address'));
//this must be the field ID for your return email, NOT the email address itself
//You can also use an array of email addresses to send to multiple TO addresses.
$o_form->setAutoResponderToAddress(array('*****@*****.**', '*****@*****.**'));
$o_form->setAutoResponderFromAddress('*****@*****.**');
$o_form->setAutoResponderFromName('Business Title');
$o_form->setAutoResponderReplyTo('*****@*****.**');
//Set the email subject and content
$o_form->setAutoResponderSubject('Business Name - Thanks for contacting us!');
$a_formRules[] = new FormRule(FormRuleType::minimumLength, $o_fe_username, 6);
$a_formRules[] = new FormRule(FormRuleType::maximumLength, $o_fe_username, 30);
//Additional rules for age field
$a_formRules[] = new FormRule(FormRuleType::numeric, $o_fe_age);
$a_formRules[] = new FormRule(FormRuleType::minimumValue, $o_fe_age, 18);
$a_formRules[] = new FormRule(FormRuleType::maximumValue, $o_fe_age, 100);
//additional rules for DOB
$a_formRules[] = new FormRule(FormRuleType::date, $o_fe_dob, 'dd/mm/yyyy');
//A unique case, when checking if passwords match pass the two fields as an array into the second argument.
$a_formRules[] = new FormRule(FormRuleType::minimumLength, $o_fe_userPass, 8);
//You could also specify "user_pass" for the 3rd paramater. This string would attempt to map back to the form matching that id.
$a_formRules[] = new FormRule(FormRuleType::fieldMatch, $o_fe_userPass2, $o_fe_userPass, 'Passwords do not match');
/*----------------------------*/
/*CREATE FORM AND ADD ELEMENTS*/
/*----------------------------*/
$o_form = new JsonFormBuilder($modx, 'myContactForm');
$o_form->setRedirectDocument(3);
$o_form->addRules($a_formRules);
//Specify to and from email addresses, also see replyTo, CC and BCC options.
$o_form->setEmailToName('To Name');
$o_form->setEmailToAddress($modx->getOption('emailsender'));
//You can set CC or BCC options
//$o_form->setEmailCCAddress('*****@*****.**');
//$o_form->setEmailBCCAddress('*****@*****.**');
//or you can use an array of addresses like so.
//$o_form->setEmailToAddress(array('*****@*****.**','*****@*****.**'));
$o_form->setEmailFromAddress($o_form->postVal('email_address'));
$o_form->setEmailFromName($o_form->postVal('name_full'));
$o_form->setEmailSubject('MyCompany 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);
//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(3);
$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->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>');
//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');
//After creating all your form elements add them
 /**
  * outputHTML()
  * 
  * Outputs the HTML for the element.
  * @return string 
  */
 public function outputHTML()
 {
     //day options
     $a_days = array('' => ' --- ');
     for ($a = 1; $a < 32; $a++) {
         $ordinalSuffix = date('S', strtotime('2000-01-' . $a));
         $a_days[$a . $ordinalSuffix] = $a . $ordinalSuffix;
     }
     //month options
     $s_monthStr = 'January,February,March,April,May,June,July,August,September,October,November,December';
     $a_temp = explode(',', $s_monthStr);
     $a_months = array('' => ' --- ');
     foreach ($a_temp as $opt) {
         $a_months[$opt] = $opt;
     }
     //year options
     $a_years = array();
     if ($this->_yearStart > $this->_yearEnd) {
         JsonFormBuilder::throwError('[Element: ' . $this->_id . '] Date start "' . $this->_yearStart . '" is greater than the end year "' . $this->_yearEnd . '".');
     }
     for ($a = $this->_yearStart; $a < $this->_yearEnd + 1; $a++) {
         $a_years[' ' . $a] = $a;
         //blank space here to be a number instead of an index
     }
     $a_years[''] = ' --- ';
     $a_yearsRev = array_reverse($a_years);
     $cnt = 0;
     foreach ($this->_dateFormat as $datePart) {
         $default = NULL;
         if ($datePart == 'day') {
             $selectArray = $a_days;
             $default = $this->_defaultVal0;
         }
         if ($datePart == 'month') {
             $selectArray = $a_months;
             $default = $this->_defaultVal1;
         }
         if ($datePart == 'year') {
             $selectArray = $a_yearsRev;
             $default = $this->_defaultVal2;
         }
         $drop = new JsonFormBuilder_elementSelect($this->_id . '_' . $cnt, '', $selectArray, $default);
         $s_ret .= '<span class="elementDate_' . $cnt . '">' . $drop->outputHTML() . '</span>';
         $cnt++;
     }
     return $s_ret;
 }
 /**
  * setDateFormat($value)
  * 
  * Sets the date format used by a field with a date FormRule.
  * This is generally done automatically via a FormRule.
  * @param string $value 
  */
 public function setDateFormat($value)
 {
     $value = trim($value);
     if (empty($value) === true) {
         JsonFormBuilder::throwError('[Element: ' . $this->_id . '] Date format is not valid.');
     } else {
         $this->_dateFormat = $value;
     }
 }
//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.
 /**
  * showInEmail($value) / showInEmail()
  * 
  * Sets wether the element is shown in the email or not. 
  * 
  * In some cases fields may be wanted in the form, but not in the email
  * (an example would be fields like a "Confirm Password" field).
  * 
  * If no value passed the method will return the current status.
  * 
  * @param boolean $value If true (which is in most cases default) the element will be shown in the email.
  * @return boolean
  */
 public function showInEmail($value = null)
 {
     if (func_num_args() == 0) {
         return $this->_showInEmail;
     } else {
         $this->_showInEmail = JsonFormBuilder::forceBool($value);
     }
 }