Skip to content

Commit

Permalink
Fixed issue #17223: With mandatory attribute : lost all current value…
Browse files Browse the repository at this point in the history
… when create a token (#1942)

Co-authored-by: encuestabizdevgit <devgit@encuesta.biz>
  • Loading branch information
gabrieljenik and encuestabizdevgit committed Aug 11, 2021
1 parent c6ab793 commit 79feec8
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 7 deletions.
6 changes: 5 additions & 1 deletion application/controllers/admin/tokens.php
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
22 changes: 20 additions & 2 deletions application/views/admin/token/tokenform.php
Expand Up @@ -24,9 +24,20 @@
}
?>

<div id="edittoken-error-container" class="row" style="display: none;">
<div class="alert alert-danger alert-dismissible" role="alert">
<button type="button" class="close" aria-label="Close" onclick="$('#edittoken-error-container').hide();"><span>×</span></button>
<span class="alert-content"></span>
</div>
</div>
<div class="row">
<div class="col-md-12 content-right">
<?php echo CHtml::form(array("admin/tokens/sa/{$token_subaction}/surveyid/{$surveyid}/tokenid/{$tokenid}"), 'post', array('id'=>'edittoken', 'class'=>'', 'data-trigger-validation'=>true)); ?>
<?php echo CHtml::form(array("admin/tokens/sa/{$token_subaction}/surveyid/{$surveyid}/tokenid/{$tokenid}"), 'post', array(
'id' => 'edittoken',
'class' => '',
'data-trigger-validation' => true,
'data-validation-error' => gT("Some mandatory additional attributes were left blank. Please review them.")
)); ?>
<!-- Tabs -->
<?php if( count($attrfieldnames) > 0 ):?>
<ul class="nav nav-tabs" id="edit-survey-text-element-language-selection">
Expand Down Expand Up @@ -417,7 +428,14 @@
<?php echo $attr_description['description'].($attr_description['mandatory'] == 'Y' ? '*' : '') ?>:
</label>
<div class="">
<input class='form-control' type='text' size='55' id='<?php echo $attr_name; ?>' name='<?php echo $attr_name; ?>' value='<?php if (isset($$attr_name)){echo htmlspecialchars($$attr_name, ENT_QUOTES, 'utf-8');}?>' />
<input
class='form-control<?= $attr_description['mandatory'] == 'Y' ? ' mandatory-attribute' : '' ?>'
type='text'
size='55'
id='<?php echo $attr_name; ?>'
name='<?php echo $attr_name; ?>'
value='<?php if (isset($$attr_name)){echo htmlspecialchars($$attr_name, ENT_QUOTES, 'utf-8');}?>'
/>
</div>
</div>
<?php endforeach; ?>
Expand Down
45 changes: 41 additions & 4 deletions assets/scripts/admin/tokens.js
Expand Up @@ -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
Expand All @@ -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
*/
Expand Down Expand Up @@ -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();
}
});


Expand Down

0 comments on commit 79feec8

Please sign in to comment.