Page 1 of 1

[solved] Required content blocks?

Posted: Sat May 03, 2014 6:08 am
by Cerulean
ListIt2 allows fields to be "required", which is very useful, and I understand required content blocks for pages will be built into CMSMS 2.0.

Until then, are there any modules that will allow me to create required content blocks in pages?

Or has anyone come up with workarounds to achieve required fields? I'm thinking here of hacks for the admin backend that would use Javascript to check for empty fields and then prevent the default behaviour of a click on Apply/Submit.

Any suggestions would be appreciated.

Re: Required content blocks?

Posted: Sat May 03, 2014 11:21 am
by velden
No, current versions don't have that option.

Good news is that CMSMS 2.0 will.

Re: Required content blocks?

Posted: Sun May 04, 2014 5:05 am
by Cerulean
jQuery workaround for admin backend

1. If you're using the default admin theme, create a renamed duplicate of it (i.e. do a find and replace on the files in the OneEleven theme, replacing instances of "OneEleven" with "MyTheme" or whatever).

2. Edit the theme's "pagetemplate.tpl", adding the following line after the "standard.js" file is appended in the template head.

Code: Select all

<scr*pt src="{$config.admin_url}/themes/MyTheme/includes/require.js"></scr*pt>
Edit: Replace "scr*pt" with "script" in the code above - this forum messes up the script tags despite them being inside a code block(??)

3. Save the following script as "require.js" in the "includes" folder.

Code: Select all

$(function(){
	// Add an asterisk beside labels of required fields
	$("label[for^='req_']").prepend("*");
	
	// Rename the Apply button to prevent the
	// default CMSMS AJAX script from running on click
	$("[name='apply']").attr("name","apply-validate");
	
	// If either the Submit or the renamed Apply button is clicked...	
	$("[name='apply-validate'], [name='submitbutton']").live("click",function(){
		// Remember which button was clicked
		var buttonclicked = $(this).attr("name");
		var errormessage = "";
		var submit = true;
		// Check to see if any of the required fields are empty
		$("input[name^='req_']").each(function(){
			if( $.trim($(this).val()) == ""){
				// If so, add a line to the error message
				errormessage += "Please enter a value for '" + $("label[for='" + $(this).attr("name") + "']").text() + "'\n";
				submit = false;
			}
		});
		// If fields okay and it was the renamed Apply button that was clicked...
		if(submit == true && buttonclicked == "apply-validate"){
			// Temporarily switch back to the old name and trigger the CMSMS AJAX script
			$(this).attr("name","apply").trigger("click").attr("name","apply-validate");
			return false;
		// If fields okay and it was the Submit button that was clicked
		} else if (submit == true) {
			return true;
		// If missing required fields, show the error message
		} else {
			alert(errormessage);
			return false;
		}
	});
});
4. Install MyTheme.

5. Use the prefix "req_" in the name of any content blocks you want to be required, e.g.

Code: Select all

{content block="req_myblock" label="My Required Block"}