diff --git a/application/controllers/admin/tokens.php b/application/controllers/admin/tokens.php index 0a814d435e9..70bb10509d3 100644 --- a/application/controllers/admin/tokens.php +++ b/application/controllers/admin/tokens.php @@ -833,6 +833,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); foreach ($aTokenData as $k => $v) { $token->$k = $v; @@ -841,7 +845,7 @@ public function edit($iSurveyId, $iTokenId, $ajax = false) $result = $token->save(); 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 251ddf56858..72be7d23f94 100644 --- a/application/views/admin/token/tokenform.php +++ b/application/views/admin/token/tokenform.php @@ -24,9 +24,20 @@ } ?> +
- 'edittoken', 'class'=>'', 'data-trigger-validation'=>true)); ?> + 'edittoken', + 'class' => '', + 'data-trigger-validation' => true, + '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 8d5d162215b..9d6c601782f 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 */ @@ -267,14 +297,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(); + } });