From 42713e11ba09cd51c31139e53099c32064c02ef7 Mon Sep 17 00:00:00 2001 From: encuestabizdevgit Date: Tue, 26 Oct 2021 17:02:59 -0300 Subject: [PATCH] Fixed issue #17223: With mandatory attribute : lost all current value when create a token --- application/controllers/admin/tokens.php | 6 ++- application/views/admin/token/tokenform.php | 21 +++++++++- assets/scripts/admin/tokens.js | 45 +++++++++++++++++++-- 3 files changed, 65 insertions(+), 7 deletions(-) diff --git a/application/controllers/admin/tokens.php b/application/controllers/admin/tokens.php index 404c826e688..eb1bfa1d1d1 100644 --- a/application/controllers/admin/tokens.php +++ b/application/controllers/admin/tokens.php @@ -729,6 +729,10 @@ public function edit($iSurveyId, $iTokenId, $ajax = false) $aTokenData[$attr_name] = $request->getPost($attr_name); } + if (!empty($sOutput)) { + \ls\ajax\AjaxHelper::outputError($sOutput); + } + $token = Token::model($iSurveyId)->findByPk($iTokenId); $token->decrypt(); foreach ($aTokenData as $k => $v) { @@ -738,7 +742,7 @@ public function edit($iSurveyId, $iTokenId, $ajax = false) $result = $token->encryptSave(true); if ($result) { - \ls\ajax\AjaxHelper::outputSuccess($sOutput . gT('The survey participant was successfully updated.')); + \ls\ajax\AjaxHelper::outputSuccess(gT('The survey participant was successfully updated.')); } else { $errors = $token->getErrors(); $firstError = reset($errors); diff --git a/application/views/admin/token/tokenform.php b/application/views/admin/token/tokenform.php index fb51bc5fc4e..50cf94d6a98 100644 --- a/application/views/admin/token/tokenform.php +++ b/application/views/admin/token/tokenform.php @@ -24,9 +24,19 @@ } ?> +
- 'edittoken', 'class'=>'')); ?> + 'edittoken', + 'class' => '', + 'data-validation-error' => gT("Some mandatory additional attributes were left blank. Please review them.") + )); ?> 0 ):?>
diff --git a/assets/scripts/admin/tokens.js b/assets/scripts/admin/tokens.js index 76f96a6e475..782dac07234 100644 --- a/assets/scripts/admin/tokens.js +++ b/assets/scripts/admin/tokens.js @@ -149,8 +149,11 @@ function submitEditToken(){ success : function(result, stat) { if (result.success) { $modal.modal('hide'); - } - else { + } else { + var errorMsg = result.error.message ? result.error.message : result.error; + if (!errorMsg) errorMsg = "Unexpected error"; + showError(errorMsg); + return; } // Using Try/Catch here to catch errors if there is no grid @@ -174,6 +177,33 @@ function submitEditToken(){ }); } +function showError(msg) { + $('#edittoken-error-container .alert-content').html(msg); + $('#edittoken-error-container').show(); +} + +/** + * Validates that mandatory additional attributes are filled + */ +function validateAdditionalAttributes() { + const validationErrorMsg = $('#edittoken').attr('data-validation-error'); + + let valid = true; + $('.mandatory-attribute').each(function () { + let value = $(this).val(); + if (value === null || value === "") { + valid = false; + if (!$('#custom').is(':visible')) { + $('.nav-tabs a[href="#custom"]').tab('show'); + } + showError(validationErrorMsg); + $(this).trigger('invalid'); + return false; + } + }); + return valid; +} + /** * Scroll the pager and the footer when scrolling horizontally */ @@ -252,14 +282,21 @@ $(document).on('ready pjax:scriptcomplete', function(){ if($('#editTokenModal').length > 0 ){ event.preventDefault(); submitEditToken(); + return; + } + if (!validateAdditionalAttributes()) { + event.preventDefault(); + return false; } }); /** * Save token */ - $("#save-edittoken").click(function(){ - submitEditToken(); + $("#save-edittoken").off('click.token-save').on('click.token-save', function() { + if (validateAdditionalAttributes()) { + submitEditToken(); + } });