Skip to content

Commit

Permalink
Merge pull request #73 from sepiariver/develop
Browse files Browse the repository at this point in the history
JSON output options for errors and hooks
  • Loading branch information
joeke committed Mar 11, 2016
2 parents 7d7b95e + 8aa0feb commit 7104726
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
20 changes: 20 additions & 0 deletions core/components/formit/model/formit/fihooks.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ function __construct(FormIt &$formit,array $config = array(),$type = '') {
'mathOp1Field' => 'op1',
'mathOp2Field' => 'op2',
'mathOperatorField' => 'operator',
'hookErrorJsonOutputPlaceholder' => ''
),$config);
$this->type = $type;
}
Expand Down Expand Up @@ -691,16 +692,35 @@ public function math(array $fields = array()) {
*/
public function processErrors() {
$errors = array();
$jsonerrors = array();
$jsonOutputPlaceholder = $this->config['hookErrorJsonOutputPlaceholder'];
if (!empty($jsonOutputPlaceholder)) {
$jsonerrors = array(
'errors' => array(),
'success' => false,
'message' => '',
);
}

$placeholderErrors = $this->getErrors();
foreach ($placeholderErrors as $key => $error) {
$errors[$key] = str_replace('[[+error]]',$error,$this->config['errTpl']);
if (!empty($jsonOutputPlaceholder)) $jsonerrors['errors'][$key] = $errors[$key];
}
$this->modx->toPlaceholders($errors,$this->config['placeholderPrefix'].'error');

$errorMsg = $this->getErrorMessage();
if (!empty($errorMsg)) {
$this->modx->setPlaceholder($this->config['placeholderPrefix'].'error_message',$errorMsg);
if (!empty($jsonOutputPlaceholder)) {
$jsonerrors['message'] = $errorMsg;
}
}
if (!empty($jsonOutputPlaceholder)) {
$jsonoutput = $this->modx->toJSON($jsonerrors);
$this->modx->setPlaceholder($jsonOutputPlaceholder,$jsonoutput);
}

}

/**
Expand Down
14 changes: 11 additions & 3 deletions core/components/formit/model/formit/fivalidator.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ function __construct(FormIt &$formit,array $config = array()) {
'placeholderPrefix' => 'fi.',
'validationErrorBulkTpl' => '<li>[[+error]]</li>',
'validationErrorBulkSeparator' => "\n",
'validationErrorBulkFormatJson' => false,
'validationErrorMessage' => '<p class="error">A form validation error occurred. Please check the values you have entered.</p>',
'use_multibyte' => (boolean)$this->modx->getOption('use_multibyte',null,false),
'trimValuesBeforeValidation' => (boolean)$this->modx->getOption('trimValuesBeforeValidation',$this->formit->config,true),
Expand Down Expand Up @@ -730,11 +731,18 @@ public function _getErrorMessage($field,$parameter,$lexiconKey,array $properties
public function processErrors() {
$this->modx->toPlaceholders($this->getErrors(),$this->config['placeholderPrefix'].'error');
$bulkErrTpl = $this->getOption('validationErrorBulkTpl');
$rawErrs = $this->getRawErrors();
$errs = array();
foreach ($this->getRawErrors() as $field => $err) {
$errs[] = str_replace(array('[[+field]]','[[+error]]'),array($field,$err),$bulkErrTpl);
$formatJson = $this->getOption('validationErrorBulkFormatJson');
if ($formatJson) {
$errs = '';
$errs = $this->modx->toJSON($rawErrs);
} else {
foreach ($rawErrs as $field => $err) {
$errs[] = str_replace(array('[[+field]]','[[+error]]'),array($field,$err),$bulkErrTpl);
}
$errs = implode($this->getOption('validationErrorBulkSeparator'),$errs);
}
$errs = implode($this->getOption('validationErrorBulkSeparator'),$errs);
$validationErrorMessage = str_replace('[[+errors]]',$errs,$this->getOption('validationErrorMessage'));
$this->modx->setPlaceholder($this->getOption('placeholderPrefix').'validation_error',true);
$this->modx->setPlaceholder($this->getOption('placeholderPrefix').'validation_error_message',$validationErrorMessage);
Expand Down

0 comments on commit 7104726

Please sign in to comment.