From f0306cdeda752fe39ceba2522a772580c7155dba Mon Sep 17 00:00:00 2001 From: "deb.monish" Date: Fri, 29 Apr 2016 16:37:53 +0530 Subject: [PATCH] CRM-18427: Submitted custom data values not reloaded on form when validation fails --- CRM/Core/BAO/CustomGroup.php | 29 ++++++++++++++++++++++++++--- templates/CRM/common/customData.tpl | 3 +++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/CRM/Core/BAO/CustomGroup.php b/CRM/Core/BAO/CustomGroup.php index d56c7ed18bdd..37bf8dcbe21a 100644 --- a/CRM/Core/BAO/CustomGroup.php +++ b/CRM/Core/BAO/CustomGroup.php @@ -1377,7 +1377,13 @@ public static function setDefaults(&$groupTree, &$defaults, $viewMode = FALSE, $ } } else { - $checkedValue = explode(CRM_Core_DAO::VALUE_SEPARATOR, substr($value, 1, -1)); + if (is_array($value) && count($value)) { + CRM_Utils_Array::formatArrayKeys($value); + $checkedValue = $value; + } + else { + $checkedValue = explode(CRM_Core_DAO::VALUE_SEPARATOR, substr($value, 1, -1)); + } foreach ($customOption as $val) { if (in_array($val['value'], $checkedValue)) { if ($field['html_type'] == 'CheckBox') { @@ -1820,7 +1826,13 @@ public static function createTable($group) { */ public static function formatGroupTree(&$groupTree, $groupCount = 1, &$form) { $formattedGroupTree = array(); - $uploadNames = array(); + $uploadNames = $formValues = array(); + + // JSON encoded submitted form values passed via URL + $submittedValues = CRM_Utils_Request::retrieve('post', 'String'); + if (!empty($submittedValues)) { + $submittedValues = json_decode($submittedValues, TRUE); + } foreach ($groupTree as $key => $value) { if ($key === 'info') { @@ -1846,8 +1858,15 @@ public static function formatGroupTree(&$groupTree, $groupCount = 1, &$form) { // add field information foreach ($value['fields'] as $k => $properties) { $properties['element_name'] = "custom_{$k}_-{$groupCount}"; + if (isset($submittedValues[$properties['element_name']])) { + $properties['element_value'] = $submittedValues[$properties['element_name']]; + } + elseif ($value = CRM_Utils_Request::retrieve($properties['element_name'], 'String', $form, FALSE, NULL, 'POST')) { + $formValues[$properties['element_name']] = $value; + } if (isset($properties['customValue']) && - !CRM_Utils_System::isNull($properties['customValue']) + !CRM_Utils_System::isNull($properties['customValue']) && + !isset($properties['element_value']) ) { if (isset($properties['customValue'][$groupCount])) { $properties['element_name'] = "custom_{$k}_{$properties['customValue'][$groupCount]['id']}"; @@ -1867,6 +1886,10 @@ public static function formatGroupTree(&$groupTree, $groupCount = 1, &$form) { } if ($form) { + if (count($formValues)) { + $form->assign('submittedValues', json_encode($formValues)); + } + // hack for field type File $formUploadNames = $form->get('uploadNames'); if (is_array($formUploadNames)) { diff --git a/templates/CRM/common/customData.tpl b/templates/CRM/common/customData.tpl index 71a737553f73..5118d7b0e421 100644 --- a/templates/CRM/common/customData.tpl +++ b/templates/CRM/common/customData.tpl @@ -54,6 +54,9 @@ {if $entityID} dataUrl += '&entityID=' + '{$entityID}'; {/if} + {if $submittedValues} + dataUrl += '&post=' + '{$submittedValues}'; + {/if} {literal} if (!cgCount) {