Help - Search - Members - Calendar
Full Version: Prevent entry submissions with no category
Movable Type Community Forum > Additional Resources > Tips and Tricks
T-DoG
How can I prevent authors from creating entries that have no category assignment?  Perhaps some javascript code can be inserted at the Create New Entry page to prevent the author from hitting SAVE if he did not select a category?

Thanks.
loopyca
Me interested as well

Loopy
T-DoG
I tried to borrow some code from here to check the dropdown category list:

http://www5.brinkster.com/hiflyer/jscript/formchecktest.htm

but I must have done something wrong, since it didn't seem to work and even broke the shortcuts (B, I, U, URL) javascript.

Can someone who knows Javascript help?  Thanks.
T-DoG
Wheee, I got it to work.  For those who are interested, I borrowed the code here:

http://developer.irt.org/script/360.htm
girlie
Maybe share the particulars of how exactly you integrated that code into MT?
T-DoG
Sorry about that.  I really never learned javascript syntax so I just searched on Google for existing code.  Here's what I put into edit_entry.tmpl.

Place the following somewhere in the javascript segment of the file:

CODE
function validate() {
   var valid = true, output = '';

   if (document.MAIN.title.value.length == 0) {
       valid = false;
       output = 'You cannot leave the title blank.\n';
   }

   if (document.MAIN.category_id.selectedIndex == 0) {
       valid = false;
       output += 'Select a category before saving the entry.\n';
   }

   if (document.MAIN.text.value.length == 0) {
       valid = false;
       output += 'Type your entry in the Main Entry Text box.';
   }

   if (!valid)
       alert(output);

   return valid;
}


It's exactly the same as the code here:
http://developer.irt.org/script/360.htm
except I changed the variables to match those in MT and I added the check for a non-empty Title textbox.

Now find
CODE
<form name ="MAIN" method="post" action="<TMPL_VAR NAME=SCRIPT_URL>">

and replace it with
CODE
<form name ="MAIN" method="post" action="<TMPL_VAR NAME=SCRIPT_URL>" onSubmit="return validate()">


That's it.  I hope I didn't break anything along the way.  The code is simple enough for silly ol' me to understand and I think it's all correct.  :p
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2010 Invision Power Services, Inc.