Check out the LimeSurvey source code on GitHub!

Question re: "Making one (or more) rows of an array mandatory"

More
5 years 9 months ago #61235 by jackywu
Hi All,

I was browsing through the Workaround section and found a way to "Making one (or more) rows of an array mandatory". I tried it and it works fine in blocking further user input when a mandatory row is empty. However when I re-enter the data into the mandatory row, the "next" button remains disable. Do I have to explicitly reactivate the "next" button? If so, how should I proceed?

Thanks,
Jack

Please Log in to join the conversation.

More
5 years 9 months ago #61237 by jackywu
FYI, this was the script I added to the array question:

<script type="text/javascript" charset="utf-8">

$(document).ready(function() {

// Interrupt the submit function
$('form#limesurvey').submit(function(){

var empty = 0;

var warningText = 'Please complete the highlighted inputs.';

// Call the mandatory row function with question IDs and row numbers
mandatoryRow(QQ, 1);
mandatoryRow(QQ, 3);

// A function to render rows of an array mandatory
function mandatoryRow(qID, rowNum) {

$( 'div#question'+qID+' table.question tbody[id^="javatbd"]:eq('+Number(rowNum - 1)+') input[type="text"]' ).each(function(i) {
if ( $( this ).val() == '' ) {
$( this ).css('background-color', 'pink');
empty = 1;
}
else {
$( this ).css('background-color', '#FFFFFF');
}
});
}

if ( empty == 1 ) {
alert (warningText);
return false;
}
else {
return true;
}
});
});
</script>

Please Log in to join the conversation.

More
5 years 9 months ago - 5 years 9 months ago #61258 by tpartner
This script doesn't disable the "Next" button, it simply interrupts the next/submit function and checks for inputs in the selected array rows. Additionally, you need to replace "QQ" (lines 13 and 14) with the array question ID .
<script type="text/javascript" charset="utf-8">
 
	$(document).ready(function() {
 
		// Interrupt the submit function
		$('form#limesurvey').submit(function(){
 
			var empty = 0;
 
			var warningText = 'Please complete the highlighted inputs.';
 
			// Call the mandatory row function with question IDs and row numbers
			mandatoryRow(QQ, 1);
			mandatoryRow(QQ, 3);
 
			// A function to render rows of an array mandatory
			function mandatoryRow(qID, rowNum) {
 
				$( 'div#question'+qID+' table.question tbody[id^="javatbd"]:eq('+Number(rowNum - 1)+') input[type="text"]' ).each(function(i) {
					if ( $( this ).val() == '' ) {
						$( this ).css('background-color', 'pink');
						empty = 1;
					}
					else {
						$( this ).css('background-color', '#FFFFFF');
					}
				});
			}
 
			if ( empty == 1 ) {
				alert (warningText);
				return false;
			}
			else {
				return true;
			}
		});
	});
 
</script>

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last Edit: 5 years 9 months ago by tpartner.

Please Log in to join the conversation.

More
5 years 8 months ago - 5 years 8 months ago #61335 by jackywu
Thanks Tony.

Yes I actually did enter the qID (2) as followed:
// Call the mandatory row function with question IDs and row numbers
mandatoryRow(2, 1);

For some reason all the navigation buttons (Prev, Next...etc) are disabled after I click Next with the mandatory fields left empty.
Last Edit: 5 years 8 months ago by jackywu.

Please Log in to join the conversation.

More
5 years 8 months ago #61336 by jackywu
I guess both the 'Next' and the 'Submit' buttons are both considered a submit action am I correct?

Please Log in to join the conversation.

More
5 years 8 months ago #61420 by tpartner

For some reason all the navigation buttons (Prev, Next...etc) are disabled after I click Next with the mandatory fields left empty.

Do you have any other scripts running? Can you activatea sample survey?

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.

Please Log in to join the conversation.

More
5 years 8 months ago - 5 years 8 months ago #61447 by jackywu
Yes, I have a Custom_On_Load to do some variable column width settings.

And I was able to activate the survey as well.

It seems like whenever I do a 'return false', the navigation buttons get disabled.
Last Edit: 5 years 8 months ago by jackywu.

Please Log in to join the conversation.

More
5 years 8 months ago - 5 years 8 months ago #61480 by tpartner
Oh, I see what's going on.

A "feature" to disable the navigation buttons after clicking has been added to 1.91. Unfortunately, this script destroys all workarounds that interrupt the Next/Submit function.

A quick fix until I can come up with something more elegant would be to override the "feature" by adding the following to the very end of endpage.pstpl.
<script type="text/javascript" charset="utf-8">
 
	$(document).ready(function() {
 
		$('form#limesurvey').submit(function() {
			$('input[type=button], input[type=submit]', this).attr('disabled', '');
		});
 
	});
 
</script>

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last Edit: 5 years 8 months ago by tpartner.

Please Log in to join the conversation.

More
5 years 8 months ago #61526 by jackywu
Thanks Tony, that worked like a charm!

I like how you put "" around the word feature haha.

Please Log in to join the conversation.

More
5 years 8 months ago #61527 by tpartner
Yeah, I don' think it's necessary and it has destroyed many workarounds.

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.

Please Log in to join the conversation.

Imprint                   Privacy policy         General Terms & Conditions         Revocation information and revocation form