Skip to content

Commit 7104726

Browse files
committed
Merge pull request #73 from sepiariver/develop
JSON output options for errors and hooks
2 parents 7d7b95e + 8aa0feb commit 7104726

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

core/components/formit/model/formit/fihooks.class.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ function __construct(FormIt &$formit,array $config = array(),$type = '') {
9292
'mathOp1Field' => 'op1',
9393
'mathOp2Field' => 'op2',
9494
'mathOperatorField' => 'operator',
95+
'hookErrorJsonOutputPlaceholder' => ''
9596
),$config);
9697
$this->type = $type;
9798
}
@@ -691,16 +692,35 @@ public function math(array $fields = array()) {
691692
*/
692693
public function processErrors() {
693694
$errors = array();
695+
$jsonerrors = array();
696+
$jsonOutputPlaceholder = $this->config['hookErrorJsonOutputPlaceholder'];
697+
if (!empty($jsonOutputPlaceholder)) {
698+
$jsonerrors = array(
699+
'errors' => array(),
700+
'success' => false,
701+
'message' => '',
702+
);
703+
}
704+
694705
$placeholderErrors = $this->getErrors();
695706
foreach ($placeholderErrors as $key => $error) {
696707
$errors[$key] = str_replace('[[+error]]',$error,$this->config['errTpl']);
708+
if (!empty($jsonOutputPlaceholder)) $jsonerrors['errors'][$key] = $errors[$key];
697709
}
698710
$this->modx->toPlaceholders($errors,$this->config['placeholderPrefix'].'error');
699711

700712
$errorMsg = $this->getErrorMessage();
701713
if (!empty($errorMsg)) {
702714
$this->modx->setPlaceholder($this->config['placeholderPrefix'].'error_message',$errorMsg);
715+
if (!empty($jsonOutputPlaceholder)) {
716+
$jsonerrors['message'] = $errorMsg;
717+
}
703718
}
719+
if (!empty($jsonOutputPlaceholder)) {
720+
$jsonoutput = $this->modx->toJSON($jsonerrors);
721+
$this->modx->setPlaceholder($jsonOutputPlaceholder,$jsonoutput);
722+
}
723+
704724
}
705725

706726
/**

core/components/formit/model/formit/fivalidator.class.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ function __construct(FormIt &$formit,array $config = array()) {
6868
'placeholderPrefix' => 'fi.',
6969
'validationErrorBulkTpl' => '<li>[[+error]]</li>',
7070
'validationErrorBulkSeparator' => "\n",
71+
'validationErrorBulkFormatJson' => false,
7172
'validationErrorMessage' => '<p class="error">A form validation error occurred. Please check the values you have entered.</p>',
7273
'use_multibyte' => (boolean)$this->modx->getOption('use_multibyte',null,false),
7374
'trimValuesBeforeValidation' => (boolean)$this->modx->getOption('trimValuesBeforeValidation',$this->formit->config,true),
@@ -730,11 +731,18 @@ public function _getErrorMessage($field,$parameter,$lexiconKey,array $properties
730731
public function processErrors() {
731732
$this->modx->toPlaceholders($this->getErrors(),$this->config['placeholderPrefix'].'error');
732733
$bulkErrTpl = $this->getOption('validationErrorBulkTpl');
734+
$rawErrs = $this->getRawErrors();
733735
$errs = array();
734-
foreach ($this->getRawErrors() as $field => $err) {
735-
$errs[] = str_replace(array('[[+field]]','[[+error]]'),array($field,$err),$bulkErrTpl);
736+
$formatJson = $this->getOption('validationErrorBulkFormatJson');
737+
if ($formatJson) {
738+
$errs = '';
739+
$errs = $this->modx->toJSON($rawErrs);
740+
} else {
741+
foreach ($rawErrs as $field => $err) {
742+
$errs[] = str_replace(array('[[+field]]','[[+error]]'),array($field,$err),$bulkErrTpl);
743+
}
744+
$errs = implode($this->getOption('validationErrorBulkSeparator'),$errs);
736745
}
737-
$errs = implode($this->getOption('validationErrorBulkSeparator'),$errs);
738746
$validationErrorMessage = str_replace('[[+errors]]',$errs,$this->getOption('validationErrorMessage'));
739747
$this->modx->setPlaceholder($this->getOption('placeholderPrefix').'validation_error',true);
740748
$this->modx->setPlaceholder($this->getOption('placeholderPrefix').'validation_error_message',$validationErrorMessage);

0 commit comments

Comments
 (0)