Skip to content

Commit

Permalink
Fixed issue #05424: validation fails on mandatory comment field in li…
Browse files Browse the repository at this point in the history
…st(radio)

Dev removed all onsubmit popup alerts saying that other comment had to be filled.  Replaced with EM-based validation and CSS  styling to indicate that the other comment needs to be filled.

git-svn-id: file:///Users/Shitiz/Downloads/lssvn/source/limesurvey_yii@12323 b72ed6b6-b9f8-46b5-92b4-906544132732
  • Loading branch information
tmswhite committed Feb 3, 2012
1 parent dcbf04d commit 959f488
Show file tree
Hide file tree
Showing 12 changed files with 280 additions and 92 deletions.
128 changes: 118 additions & 10 deletions application/helpers/expressions/em_manager_helper.php
Expand Up @@ -1162,6 +1162,51 @@ public function _CreateSubQLevelRelevanceAndValidationEqns($onlyThisQseq=NULL)
$multiflexible_max='';
}

// other_comment_mandatory
// Validation:= sqN <= value (which could be an expression).
if (isset($qattr['other_comment_mandatory']) && trim($qattr['other_comment_mandatory']) != '')
{
$other_comment_mandatory = $qattr['other_comment_mandatory'];
$eqn='';
if ($other_comment_mandatory == '1')
{
$sgqa = $qinfo['sgqa'];
switch ($type)
{
// TODO oddly, the other field has value of 0 when empty instead of "", so cheating and looking for strlen > 1
case '!': //List - dropdown
case 'L': //LIST drop-down/radio-button list
$eqn = "(" . $sgqa . ".NAOK!='-oth-' || (" . $sgqa . ".NAOK=='-oth-' && strlen(trim(" . $sgqa . "other.NAOK))>1))";
break;
case 'P': //Multiple choice with comments checkbox + text
$eqn = "(is_empty(trim(" . $sgqa . "other.NAOK)) || (!is_empty(trim(" . $sgqa . "other.NAOK)) && strlen(trim(" . $sgqa . "othercomment.NAOK))>1))";
break;
default:
break;
}
}
if ($eqn != '')
{
if (!isset($validationEqn[$questionNum]))
{
$validationEqn[$questionNum] = array();
}
$validationEqn[$questionNum][] = array(
'qtype' => $type,
'type' => 'other_comment_mandatory',
'class' => 'other_comment_mandatory',
'eqn' => $eqn,
'qid' => $questionNum,
);
}
}
else
{
$other_comment_mandatory = '';
}



// show_totals
// TODO - create equations for these?
Expand Down Expand Up @@ -1500,6 +1545,12 @@ public function _CreateSubQLevelRelevanceAndValidationEqns($onlyThisQseq=NULL)
$qtips['sum_range']=sprintf($this->gT("The sum must equal %s"),'{'.$equals_num_value.'}');
}

// other comment mandatory
if ($other_comment_mandatory!='')
{
$qtips['other_comment_mandatory']=$this->gT('Please also fill in the "other comment" field.');
}

// regular expression validation
if ($preg!='')
{
Expand Down Expand Up @@ -1982,10 +2033,32 @@ private function setVariableAndTokenMappingsForExpressionManager($surveyid,$forc
$jsVarName = 'java' . $sgqa;
break;
case '!': //List - dropdown
if (preg_match("/other$/",$sgqa))
{
$jsVarName = 'java' . $sgqa;
$jsVarName_on = 'othertext' . substr($sgqa,0,-5);
}
else
{
$jsVarName = 'java' . $sgqa;
$jsVarName_on = $jsVarName;
}
break;
case 'L': //LIST drop-down/radio-button list
if (preg_match("/other$/",$sgqa))
{
$jsVarName = 'java' . $sgqa;
$jsVarName_on = 'answer' . $sgqa . "text";
}
else
{
$jsVarName = 'java' . $sgqa;
$jsVarName_on = $jsVarName;
}
break;
case '5': //5 POINT CHOICE radio-buttons
case 'G': //GENDER drop-down list
case 'I': //Language Question
case 'L': //LIST drop-down/radio-button list
case 'Y': //YES/NO radio-buttons
case '*': //Equation
case '1': //Array (Flexible Labels) dual scale
Expand Down Expand Up @@ -2024,7 +2097,7 @@ private function setVariableAndTokenMappingsForExpressionManager($surveyid,$forc
}
break;
}
if (!is_null($rowdivid) || $type == 'L' || $type == 'N' || !is_null($preg)) {
if (!is_null($rowdivid) || $type == 'L' || $type == 'N' || $type == '!' || !is_null($preg)) {
if (!isset($q2subqInfo[$questionNum])) {
$q2subqInfo[$questionNum] = array(
'qid' => $questionNum,
Expand All @@ -2040,7 +2113,7 @@ private function setVariableAndTokenMappingsForExpressionManager($surveyid,$forc
if (!isset($q2subqInfo[$questionNum]['subqs'])) {
$q2subqInfo[$questionNum]['subqs'] = array();
}
if ($type == 'L')
if ($type == 'L' || $type == '!')
{
if (!is_null($ansArray))
{
Expand Down Expand Up @@ -4963,8 +5036,10 @@ static function GetRelevanceAndTailoringJavaScript()
if (count($validationEqns) > 0) {
$_hasSumRange=false;
$_hasOtherValidation=false;
$_hasOther2Validation=false;
$jsParts[] = " isValidSum" . $arg['qid'] . "=true;\n"; // assume valid until proven otherwise
$jsParts[] = " isValidOther" . $arg['qid'] . "=true;\n"; // assume valid until proven otherwise
$jsParts[] = " isValidOtherComment" . $arg['qid'] . "=true;\n"; // assume valid until proven otherwise
foreach ($validationEqns as $vclass=>$validationEqn)
{
if ($validationEqn == '') {
Expand All @@ -4974,6 +5049,10 @@ static function GetRelevanceAndTailoringJavaScript()
{
$_hasSumRange = true;
}
else if ($vclass == 'other_comment_mandatory')
{
$_hasOther2Validation = true;
}
else
{
$_hasOtherValidation = true;
Expand All @@ -4993,6 +5072,9 @@ static function GetRelevanceAndTailoringJavaScript()
case 'sum_range':
$jsParts[] = " isValidSum" . $arg['qid'] . "=false;\n";
break;
case 'other_comment_mandatory':
$jsParts[] = " isValidOtherComment" . $arg['qid'] . "=false;\n";
break;
// case 'num_answers':
// case 'value_range':
// case 'sq_fn_validation':
Expand Down Expand Up @@ -5029,13 +5111,39 @@ static function GetRelevanceAndTailoringJavaScript()
break;
}

$jsParts[] = "\n if(isValidOther" . $arg['qid'] ." && isValidSum" . $arg['qid'] . "){\n";
$jsParts[]= " $('#" . $arg['qid'] . "_vmsg').removeClass('error').addClass('good');\n";
// $jsParts[]= " $('#question" . $arg['qid'] . "').removeClass('input-error');\n";
$jsParts[] = " }\n else {\n";
$jsParts[]= " $('#" . $arg['qid'] . "_vmsg').removeClass('good').addClass('error');\n";
// $jsParts[]= " $('#question" . $arg['qid'] . "').addClass('input-error');\n";
$jsParts[] = " }\n";
// color-code mandatory other comment fields
switch ($arg['type'])
{
case '!':
case 'L':
case 'P':
switch ($arg['type'])
{
case '!':
$othervar = 'othertext' . substr($arg['jsResultVar'],4,-5);
break;
case 'L':
$othervar = 'answer' . substr($arg['jsResultVar'],4) . 'text';
break;
case 'P':
$othervar = 'answer' . substr($arg['jsResultVar'],4);
break;
}
$jsParts[] = "\n if(isValidOtherComment" . $arg['qid'] . "){\n";
$jsParts[] = " $('#" . $othervar . "').addClass('em_sq_validation').removeClass('error').addClass('good');\n";
$jsParts[] = " }\n else {\n";
$jsParts[] = " $('#" . $othervar . "').addClass('em_sq_validation').removeClass('good').addClass('error');\n";
$jsParts[] = " }\n";
break;
default:
break;
}

// $jsParts[] = "\n if(isValidOther" . $arg['qid'] ." && isValidSum" . $arg['qid'] . "){\n";
// $jsParts[]= " $('#" . $arg['qid'] . "_vmsg').removeClass('error').addClass('good');\n";
// $jsParts[] = " }\n else {\n";
// $jsParts[]= " $('#" . $arg['qid'] . "_vmsg').removeClass('good').addClass('error');\n";
// $jsParts[] = " }\n";
}

if ($arg['hidden']) {
Expand Down
164 changes: 82 additions & 82 deletions application/helpers/qanda_helper.php
Expand Up @@ -1588,31 +1588,31 @@ function do_list_dropdown($ia)
$answer .= "</p>";
}

$checkotherscript = "";
if (isset($other) && $other == 'Y' && $aQuestionAttributes['other_comment_mandatory']==1)
{
$checkotherscript = "\n<script type='text/javascript'>\n"
. "\t<!--\n"
. "oldonsubmitOther_{$ia[0]} = document.limesurvey.onsubmit;\n"
. "function ensureOther_{$ia[0]}()\n"
. "{\n"
. "\tothercommentval=document.getElementById('othertext{$ia[1]}').value;\n"
. "\totherval=document.getElementById('answer{$ia[1]}').value;\n"
. "\tif (otherval == '-oth-' && othercommentval == '') {\n"
. "alert('".sprintf($clang->gT("You've selected the \"%s\" answer for question \"%s\". Please also fill in the accompanying \"other comment\" field.","js"),trim(javascriptEscape($othertext,true,true)),trim(javascriptEscape($ia[3],true,true)))."');\n"
. "return false;\n"
. "\t}\n"
. "\telse {\n"
. "if(typeof oldonsubmitOther_{$ia[0]} == 'function') {\n"
. "\treturn oldonsubmitOther_{$ia[0]}();\n"
. "}\n"
. "\t}\n"
. "}\n"
. "document.limesurvey.onsubmit = ensureOther_{$ia[0]};\n"
. "\t-->\n"
. "</script>\n";
}
$answer = $checkotherscript . $answer;
// $checkotherscript = "";
// if (isset($other) && $other == 'Y' && $aQuestionAttributes['other_comment_mandatory']==1)
// {
// $checkotherscript = "\n<script type='text/javascript'>\n"
// . "\t<!--\n"
// . "oldonsubmitOther_{$ia[0]} = document.limesurvey.onsubmit;\n"
// . "function ensureOther_{$ia[0]}()\n"
// . "{\n"
// . "\tothercommentval=document.getElementById('othertext{$ia[1]}').value;\n"
// . "\totherval=document.getElementById('answer{$ia[1]}').value;\n"
// . "\tif (otherval == '-oth-' && othercommentval == '') {\n"
// . "alert('".sprintf($clang->gT("You've selected the \"%s\" answer for question \"%s\". Please also fill in the accompanying \"other comment\" field.","js"),trim(javascriptEscape($othertext,true,true)),trim(javascriptEscape($ia[3],true,true)))."');\n"
// . "return false;\n"
// . "\t}\n"
// . "\telse {\n"
// . "if(typeof oldonsubmitOther_{$ia[0]} == 'function') {\n"
// . "\treturn oldonsubmitOther_{$ia[0]}();\n"
// . "}\n"
// . "\t}\n"
// . "}\n"
// . "document.limesurvey.onsubmit = ensureOther_{$ia[0]};\n"
// . "\t-->\n"
// . "</script>\n";
// }
// $answer = $checkotherscript . $answer;

$inputnames[]=$ia[1];
return array($answer, $inputnames);
Expand Down Expand Up @@ -1848,33 +1848,33 @@ function do_list_radio($ia)
$answer .= $wrapper['whole-end'].'
<input type="hidden" name="java'.$ia[1].'" id="java'.$ia[1]."\" value=\"".$_SESSION['survey_'.Yii::app()->getConfig('surveyID')][$ia[1]]."\" />\n";

$checkotherscript = "";

if (isset($other) && $other == 'Y' && $aQuestionAttributes['other_comment_mandatory']==1)
{
$checkotherscript = "<script type='text/javascript'>\n"
. "\t<!--\n"
. "oldonsubmitOther_{$ia[0]} = document.limesurvey.onsubmit;\n"
. "function ensureOther_{$ia[0]}()\n"
. "{\n"
. "\tothercommentval=document.getElementById('answer{$ia[1]}othertext').value;\n"
. "\totherval=document.getElementById('SOTH{$ia[1]}').checked;\n"
. "\tif (otherval == true && othercommentval == '') {\n"
. "alert('".sprintf($clang->gT("You've selected the \"%s\" answer for question \"%s\". Please also fill in the accompanying \"other comment\" field.","js"),trim(javascriptEscape($othertext,true,true)),trim(javascriptEscape($ia[3],true,true)))."');\n"
. "return false;\n"
. "\t}\n"
. "\telse {\n"
. "if(typeof oldonsubmitOther_{$ia[0]} == 'function') {\n"
. "\treturn oldonsubmitOther_{$ia[0]}();\n"
. "}\n"
. "\t}\n"
. "}\n"
. "document.limesurvey.onsubmit = ensureOther_{$ia[0]};\n"
. "\t-->\n"
. "</script>\n";
}

$answer = $checkotherscript . $answer;
// $checkotherscript = "";
//
// if (isset($other) && $other == 'Y' && $aQuestionAttributes['other_comment_mandatory']==1)
// {
// $checkotherscript = "<script type='text/javascript'>\n"
// . "\t<!--\n"
// . "oldonsubmitOther_{$ia[0]} = document.limesurvey.onsubmit;\n"
// . "function ensureOther_{$ia[0]}()\n"
// . "{\n"
// . "\tothercommentval=document.getElementById('answer{$ia[1]}othertext').value;\n"
// . "\totherval=document.getElementById('SOTH{$ia[1]}').checked;\n"
// . "\tif (otherval == true && othercommentval == '') {\n"
// . "alert('".sprintf($clang->gT("You've selected the \"%s\" answer for question \"%s\". Please also fill in the accompanying \"other comment\" field.","js"),trim(javascriptEscape($othertext,true,true)),trim(javascriptEscape($ia[3],true,true)))."');\n"
// . "return false;\n"
// . "\t}\n"
// . "\telse {\n"
// . "if(typeof oldonsubmitOther_{$ia[0]} == 'function') {\n"
// . "\treturn oldonsubmitOther_{$ia[0]}();\n"
// . "}\n"
// . "\t}\n"
// . "}\n"
// . "document.limesurvey.onsubmit = ensureOther_{$ia[0]};\n"
// . "\t-->\n"
// . "</script>\n";
// }
//
// $answer = $checkotherscript . $answer;

$inputnames[]=$ia[1];
return array($answer, $inputnames);
Expand Down Expand Up @@ -2986,36 +2986,36 @@ function do_multiplechoice_withcomments($ia)
// //$answer = $minanswscript . $answer;
// }

$checkotherscript = "";
//if ($other == 'Y' && $aQuestionAttributes['other_comment_mandatory']==1) //TIBO
if ($other == 'Y' && $aQuestionAttributes['other_comment_mandatory']==1) //TIBO
{
// Multiple choice with 'other' is a specific case as the checkbox isn't recorded into DB
// this means that if it is cehcked We must force the end-user to enter text in the input
// box
$checkotherscript = "<script type='text/javascript'>\n"
. "\t<!--\n"
. "oldonsubmitOther_{$ia[0]} = document.limesurvey.onsubmit;\n"
. "function ensureOther_{$ia[0]}()\n"
. "{\n"
. "\tothercommentval=document.getElementById('answer".$myfname2."').value;\n"
. "\totherval=document.getElementById('answer".$myfname."').value;\n"
. "\tif (otherval != '' && othercommentval == '') {\n"
. "alert('".sprintf($clang->gT("You've marked the \"other\" field for question \"%s\". Please also fill in the accompanying \"other comment\" field.","js"),trim(javascriptEscape($ia[3],true,true)))."');\n"
. "return false;\n"
. "\t}\n"
. "\telse {\n"
. "if(typeof oldonsubmitOther_{$ia[0]} == 'function') {\n"
. "\treturn oldonsubmitOther_{$ia[0]}();\n"
. "}\n"
. "\t}\n"
. "}\n"
. "document.limesurvey.onsubmit = ensureOther_{$ia[0]};\n"
. "\t-->\n"
. "</script>\n";
}

$answer = $checkotherscript . $answer;
// $checkotherscript = "";
// //if ($other == 'Y' && $aQuestionAttributes['other_comment_mandatory']==1) //TIBO
// if ($other == 'Y' && $aQuestionAttributes['other_comment_mandatory']==1) //TIBO
// {
// // Multiple choice with 'other' is a specific case as the checkbox isn't recorded into DB
// // this means that if it is cehcked We must force the end-user to enter text in the input
// // box
// $checkotherscript = "<script type='text/javascript'>\n"
// . "\t<!--\n"
// . "oldonsubmitOther_{$ia[0]} = document.limesurvey.onsubmit;\n"
// . "function ensureOther_{$ia[0]}()\n"
// . "{\n"
// . "\tothercommentval=document.getElementById('answer".$myfname2."').value;\n"
// . "\totherval=document.getElementById('answer".$myfname."').value;\n"
// . "\tif (otherval != '' && othercommentval == '') {\n"
// . "alert('".sprintf($clang->gT("You've marked the \"other\" field for question \"%s\". Please also fill in the accompanying \"other comment\" field.","js"),trim(javascriptEscape($ia[3],true,true)))."');\n"
// . "return false;\n"
// . "\t}\n"
// . "\telse {\n"
// . "if(typeof oldonsubmitOther_{$ia[0]} == 'function') {\n"
// . "\treturn oldonsubmitOther_{$ia[0]}();\n"
// . "}\n"
// . "\t}\n"
// . "}\n"
// . "document.limesurvey.onsubmit = ensureOther_{$ia[0]};\n"
// . "\t-->\n"
// . "</script>\n";
// }
//
// $answer = $checkotherscript . $answer;

return array($answer, $inputnames);
}
Expand Down
8 changes: 8 additions & 0 deletions templates/basic/template.css
Expand Up @@ -1255,6 +1255,14 @@ div.em_sq_fn_validation.error {
color: #FF00FF;
}

div.em_other_comment_mandatory.good {
display: none;
}
div.em_other_comment_mandatory.error {
color: #FF00FF;
display: block;
}

input.em_sq_validation.good {
}

Expand Down

0 comments on commit 959f488

Please sign in to comment.