MandatorySubject

From Request Tracker Wiki
Revision as of 15:47, 3 April 2009 by BillCole (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

This feature provides a simple way to ensure that a ticket will always have a Subject. It is implemented via JavaScript and so requires that JavaScript be enabled in browsers (or at least in the browsers of people who leave the subject blank). As such, this may not be the best approach to take for some sites, as it does not block creation of tickets with empty subjects on the server side. You can easily adapt this to validate other form elements. This patch was created against 3.6.3 source.

The following files will be patched (where <rt-root> is the root RT directory, e.g. /opt/rt3):

  • <rt-root>/share/html/index.html
  • <rt-root>/share/html/Elements/Header
  • [=<rt-root>/share/html/Elements/QuickCreate]
  • <rt-root>/share/html/Elements/Submit
  • [=<rt-root>/share/html/SelfService/Create.html]
  • <rt-root>/share/html/Ticket/Create.html
  • <rt-root>/share/html/Ticket/Modify.html
  • [=<rt-root>/share/html/Ticket/ModifyAll.html]

For each of the above files that have not already been patched, you must:

  1. recreate the directory structure under <rt-root>/local (e.g. [=<rt-root>/local/html/SelfService])
  2. copy the file from <rt-root>/share/whatever to <rt-root>/local/whatever

After that, you need to:

  1. create the file <rt-root>/subject.diff, with the contents shown below
  2. apply the patch (assuming <rt-root> == /opt/rt3):
$ cd /opt/rt3
$ patch -p0 <subject.diff

<rt-root>/subject.diff

diff -Naurb share/html/Elements/Header local/html/Elements/Header
 --- share/html/Elements/Header	2007-02-15 09:43:14.000000000 -0500
 +++ local/html/Elements/Header	2007-02-15 12:24:12.000000000 -0500
 @@ -76,6 +76,14 @@
  % }
  --></script>
 
 +% if ($JavaScript) {
 +<SCRIPT LANGUAGE="JavaScript">
 +<!--
 +<% $JavaScript |n %>
 +-->
 +</SCRIPT>
 +% }
 +
  <& /Elements/Callback, _CallbackName => 'Head', %ARGS &>
 
  </head>
 @@ -128,4 +136,5 @@
  $URL => undef
  $RSSAutoDiscovery => undef
  $onload => undef
 +$JavaScript => undef
  </%ARGS>
 diff -Naurb share/html/Elements/QuickCreate local/html/Elements/QuickCreate
 --- share/html/Elements/QuickCreate	2007-02-15 09:43:16.000000000 -0500
 +++ local/html/Elements/QuickCreate	2007-02-15 12:17:32.000000000 -0500
 @@ -45,7 +45,7 @@
  %# END BPS TAGGED BLOCK }}}
  <div class="quick-create">
  <&| /Widgets/TitleBox, title => loc('Quick ticket creation') &>
 -<form method="post" action="<%$RT::WebPath%>/index.html">
 +<form method="post" action="<%$RT::WebPath%>/index.html" name="QuickCreate">
  <input type="hidden" class="hidden" name="QuickCreate" value="1" />
  <table>
  <tr><td>
 @@ -62,7 +62,7 @@
  </tr>
  %#<tr><td colspan="3"><textarea cols="50" rows="3"></textarea></td></tr>
  </table>
 -<div align="right"><input type="submit" class="button" value="<%loc('Create')%>" /></div>
 +<div align="right"><input type="submit" class="button" value="<%loc('Create')%>" onClick="return checkform(document.QuickCreate);" /></div>
  </form>
  </&>
  </div>
 diff -Naurb share/html/Elements/Submit local/html/Elements/Submit
 --- share/html/Elements/Submit	2007-02-15 09:43:16.000000000 -0500
 +++ local/html/Elements/Submit	2007-02-15 09:43:00.000000000 -0500
 @@ -61,7 +61,7 @@
    <input type="submit" <% $Name && qq[ name="$Name"] | n %> value="<%$AlternateLabel%>" class="button" />
  % } else {
    <span class="caption"><%$Caption%></span>
 -  <input type="submit" <% $Name && qq[ name="$Name"] | n %> value="<%$Label%>" class="button" />
 +  <input type="submit" <% $Name && qq[ name="$Name"] | n %> value="<%$Label%>" class="button" <% $JavaScript && $JavaScript |n %>/>
  % }
    </div>
    <div class="submit-clear"></div>
 @@ -81,4 +81,5 @@
  $CheckboxName => ''
  $Reset => undef
  $ResetLabel => loc('Reset')
 +$JavaScript => undef
  </%ARGS>
 diff -Naurb share/html/index.html local/html/index.html
 --- share/html/index.html	2007-02-15 09:43:08.000000000 -0500
 +++ local/html/index.html	2007-02-15 12:18:02.000000000 -0500
 @@ -1,4 +1,14 @@
 -<& /Elements/Header, Title=>loc("RT at a glance"), Refresh => $session{'home_refresh_interval'} &>
 +<& /Elements/Header,
 +   Title=>loc("RT at a glance"),
 +   Refresh => $session{'home_refresh_interval'},
 +   JavaScript => q(function checkform( form ) {
 +    if (form.Subject.value == "") {
 +        alert( "You must enter a subject." );
 +        form.Subject.focus();
 +        return false;
 +    }
 +    return true;
 +} ) &>
  <!--
  % $m->out('--'.'>');
  % if (0) {
 diff -Naurb share/html/SelfService/Create.html local/html/SelfService/Create.html
 --- share/html/SelfService/Create.html	2007-02-15 09:43:20.000000000 -0500
 +++ local/html/SelfService/Create.html	2007-02-15 12:25:06.000000000 -0500
 @@ -43,9 +43,18 @@
  %# those contributions and any derivatives thereof.
  %#
  %# END BPS TAGGED BLOCK }}}
 -<& Elements/Header, Title => loc("Create a ticket") &>
 +<& Elements/Header,
 +    Title => loc("Create a ticket"),
 +	JavaScript => q(function checkform( form ) {
 +    if (form.Subject.value == "") {
 +        alert( "You must enter a subject." );
 +        form.Subject.focus();
 +        return false;
 +    }
 +    return true;
 +} ) &>
 
 -<form action="Display.html" method="post" enctype="multipart/form-data">
 +<form action="Display.html" method="post" enctype="multipart/form-data" name="TicketCreate">
  <input type="hidden" class="hidden" name="id" value="new" />
 
  <table>
 @@ -102,7 +111,9 @@
  </td>
  </tr>
  </table>
 -<& /Elements/Submit, Label => loc("Create ticket")&>
 +<& /Elements/Submit,
 +   Label => loc("Create ticket"),
 +   JavaScript => q(onClick="return checkform(document.TicketCreate);") &>
 
 
  </form>
 diff -Naurb share/html/Ticket/Create.html local/html/Ticket/Create.html
 --- share/html/Ticket/Create.html	2007-02-15 09:43:10.000000000 -0500
 +++ local/html/Ticket/Create.html	2007-02-15 12:26:08.000000000 -0500
 @@ -45,7 +45,15 @@
  %# END BPS TAGGED BLOCK }}}
  <& /Elements/Header,
      Title => loc("Create a new ticket"),
 -    onload => "hide(document.getElementById('Ticket-Create-details'));" &>
 +    onload => "hide(document.getElementById('Ticket-Create-details'));",
 +	JavaScript => q(function checkform( form ) {
 +    if (form.Subject.value == "") {
 +        alert( "You must enter a subject." );
 +        form.Subject.focus();
 +        return false;
 +    }
 +    return true;
 +} ) &>
  <& /Elements/Tabs,
      current_toptab => "Ticket/Create.html",
      Title => loc("Create a new ticket"),
 @@ -165,7 +173,9 @@
  </tr>
  </table>
  </&>
 -<& /Elements/Submit, Label => loc("Create")&>
 +<& /Elements/Submit,
 +   Label => loc("Create"),
 +   JavaScript => q(onClick="return checkform(document.TicketCreate);") &>
  </div>
 
  <div id="Ticket-Create-details">
 @@ -232,7 +242,9 @@
  </td>
  </tr>
  </table>
 -<& /Elements/Submit, Label => loc("Create") &>
 +<& /Elements/Submit,
 +   Label => loc("Create"),
 +   JavaScript => q(onClick="return checkform(document.TicketCreate);") &>
  </div>
  </form>
 
 diff -Naurb share/html/Ticket/ModifyAll.html local/html/Ticket/ModifyAll.html
 --- share/html/Ticket/ModifyAll.html	2007-02-15 09:43:12.000000000 -0500
 +++ local/html/Ticket/ModifyAll.html	2007-02-15 09:58:48.000000000 -0500
 @@ -43,7 +43,16 @@
  %# those contributions and any derivatives thereof.
  %#
  %# END BPS TAGGED BLOCK }}}
 -<& /Elements/Header, Title => loc("Ticket #[_1] Jumbo update: [_2]", $Ticket->Id, $Ticket->Subject) &>
 +<& /Elements/Header,
 +   Title => loc("Ticket #[_1] Jumbo update: [_2]", $Ticket->Id, $Ticket->Subject),
 +   JavaScript => q(function checkform( form ) {
 +    if (form.Subject.value == "") {
 +        alert( "You must enter a subject." );
 +        form.Subject.focus();
 +        return false;
 +    }
 +    return true;
 +} ) &>
  <& /Ticket/Elements/Tabs,
      Ticket => $Ticket,
      current_tab => "Ticket/ModifyAll.html?id=".$Ticket->Id,
 @@ -51,7 +60,7 @@
 
  <& /Elements/ListActions, actions => \@results &>
 
 -<form method="post" action="ModifyAll.html" enctype="multipart/form-data">
 +<form method="post" action="ModifyAll.html" enctype="multipart/form-data" name="TicketModify">
  <& /Elements/Callback, _CallbackName => 'FormStart',ARGSRef =>\%ARGS &>
  <input type="hidden" class="hidden" name="id" value="<%$Ticket->Id%>" />
 
 @@ -125,7 +134,9 @@
 
  <& /Elements/Submit,
      Label => loc('Save Changes'),
 -    Caption => loc("If you've updated anything above, be sure to"), color => "#333399" &>
 +    Caption => loc("If you've updated anything above, be sure to"),
 +	color => "#333399",
 +    JavaScript => q(onClick="return checkform(document.TicketModify);") &>
  </form>
 
  <%INIT>
 diff -Naurb share/html/Ticket/Modify.html local/html/Ticket/Modify.html
 --- share/html/Ticket/Modify.html	2007-02-15 09:43:12.000000000 -0500
 +++ local/html/Ticket/Modify.html	2007-02-15 12:26:26.000000000 -0500
 @@ -43,13 +43,22 @@
  %# those contributions and any derivatives thereof.
  %#
  %# END BPS TAGGED BLOCK }}}
 -<& /Elements/Header, Title => loc('Modify ticket #[_1]', $TicketObj->Id) &>
 +<& /Elements/Header,
 +   Title => loc('Modify ticket #[_1]', $TicketObj->Id),
 +   JavaScript => q(function checkform( form ) {
 +    if (form.Subject.value == "") {
 +        alert( "You must enter a subject." );
 +        form.Subject.focus();
 +        return false;
 +    }
 +    return true;
 +} ) &>
  <& /Ticket/Elements/Tabs,
      Ticket => $TicketObj, current_subtab => "Ticket/Modify.html?id=".$TicketObj->Id,
      Title => loc('Modify ticket #[_1]', $TicketObj->Id) &>
 
  <& /Elements/ListActions, actions => \@results &>
 -<form method="post" action="Modify.html" enctype="multipart/form-data">
 +<form method="post" action="Modify.html" enctype="multipart/form-data" name="TicketModify">
  <& /Elements/Callback, _CallbackName => 'FormStart',ARGSRef =>\%ARGS &>
  <input type="hidden" class="hidden" name="id" value="<%$TicketObj->Id%>" />
  <&| /Widgets/TitleBox, title => loc('Modify ticket #[_1]',$TicketObj->Id) &>
 @@ -57,7 +66,11 @@
  <& Elements/EditCustomFields, TicketObj => $TicketObj &>
  </&>
 
 -<& /Elements/Submit, Label => loc('Save Changes'), Caption => loc("If you've updated anything above, be sure to"), color => "#993333" &>
 +<& /Elements/Submit,
 +   Label => loc('Save Changes'),
 +   Caption => loc("If you've updated anything above, be sure to"),
 +   color => "#993333",
 +   JavaScript => q(onClick="return checkform(document.TicketModify);") &>
  </form>
  <%INIT>
 
 
 

There's basically a couple of parts to this patch. The most important is at the very top of the diff, the one that modifies local/html/Elements/Header, and it adds in a block of code that sticks the <SCRIPT> tag into the HTML, if it's passed into the Mason Element. This is going to be the function definition of the code that actually checks your input. You have to provide this function definition, and it would look something like this:

function checkform( form ) {
   if (form.Subject.value == "") {
      alert( "You must enter a subject." );
      form.Subject.focus();
      return false;
   }

   return true;
}

The next change is to local/html/Elements/Submit to tell it to call the function just described. Again, you have to provide the code, and it will look like this:

<& /Elements/Submit,
   Label => loc("Create ticket"),
   JavaScript => q(onClick="return checkform(document.TicketCreate);")
&>

Next, find a FORM and a form element that you want to add a check to. Modify the FORM to give it a name if it doesn't have one already, e.g. <form action="Display.html" method="post" enctype="multipart/form-data" name="TicketCreate">

The input field you want to check should have a name; note it (e.g. Subject). You will need that name for the checkform function.

The Elements/Header for that page should be modified to pass in the checkform function described above:

JavaScript => q(function checkform( form ) {
   if (form.Subject.value == "") {
      alert( "You must enter a subject." );
      form.Subject.focus();
      return false;
   }

   return true;
}

Next find Submit button for that form and modify it as directed above in the Elements/Submit section. Some forms don't use Elements/Submit, and you would have to code up the input manually, like this:

<input type="submit" class="button" value="<%loc('Create')%>" onClick="return checkform(document.QuickCreate);" />

That's all there is to it! If you have any problems with the patch, please let me know -- JoeCasadonte