Difference between revisions of "MandatorySubject"

From Request Tracker Wiki
Jump to navigation Jump to search
 
m (3 revisions imported)
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
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.
Mandatory subjects can be provided by the [http://bestpractical.com/rt/extensions.html#RT::Extension::MandatorySubject MandatorySubject extension].
 
The following files will be patched (where <code>&lt;rt-root&gt;</code> is the root RT directory, e.g. <code>/opt/rt3</code>):
 
* <code>&lt;rt-root&gt;/share/html/index.html</code>
* <code>&lt;rt-root&gt;/share/html/Elements/Header</code>
* [=&lt;rt-root&gt;/share/html/Elements/QuickCreate]
* <code>&lt;rt-root&gt;/share/html/Elements/Submit</code>
* [=&lt;rt-root&gt;/share/html/SelfService/Create.html]
* <code>&lt;rt-root&gt;/share/html/Ticket/Create.html</code>
* <code>&lt;rt-root&gt;/share/html/Ticket/Modify.html</code>
* [=&lt;rt-root&gt;/share/html/Ticket/ModifyAll.html]
 
For each of the above files that have not already been patched, you must:
 
# recreate the directory structure under <code>&lt;rt-root&gt;/local</code> (e.g. [=&lt;rt-root&gt;/local/html/SelfService])
# copy the file from <code>&lt;rt-root&gt;/share/whatever</code> to <code>&lt;rt-root&gt;/local/whatever</code>
 
After that, you need to:
 
# create the file <code>&lt;rt-root&gt;/subject.diff</code>, with the contents shown below
# apply the patch (assuming <code>&lt;rt-root&gt; == /opt/rt3</code>):
 
$ cd /opt/rt3
$ patch -p0 &lt;subject.diff
 
== &lt;rt-root&gt;/subject.diff ==
 
<nowiki>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 @@
  % }
  --&gt;&lt;/script&gt;
+% if ($JavaScript) {
+&lt;SCRIPT LANGUAGE="JavaScript"&gt;
+&lt;!--
+&lt;% $JavaScript |n %&gt;
+--&gt;
+&lt;/SCRIPT&gt;
+% }
+
  &lt;&amp; /Elements/Callback, _CallbackName =&gt; 'Head', %ARGS &amp;&gt;
  &lt;/head&gt;
@@ -128,4 +136,5 @@
  $URL =&gt; undef
  $RSSAutoDiscovery =&gt; undef
  $onload =&gt; undef
+$JavaScript =&gt; undef
  &lt;/%ARGS&gt;
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 }}}
  &lt;div class="quick-create"&gt;
  &lt;&amp;| /Widgets/TitleBox, title =&gt; loc('Quick ticket creation') &amp;&gt;
-&lt;form method="post" action="&lt;%$RT::WebPath%&gt;/index.html"&gt;
+&lt;form method="post" action="&lt;%$RT::WebPath%&gt;/index.html" name="QuickCreate"&gt;
  &lt;input type="hidden" class="hidden" name="QuickCreate" value="1" /&gt;
  &lt;table&gt;
  &lt;tr&gt;&lt;td&gt;
@@ -62,7 +62,7 @@
  &lt;/tr&gt;
  %#&lt;tr&gt;&lt;td colspan="3"&gt;&lt;textarea cols="50" rows="3"&gt;&lt;/textarea&gt;&lt;/td&gt;&lt;/tr&gt;
  &lt;/table&gt;
-&lt;div align="right"&gt;&lt;input type="submit" class="button" value="&lt;%loc('Create')%&gt;" /&gt;&lt;/div&gt;
+&lt;div align="right"&gt;&lt;input type="submit" class="button" value="&lt;%loc('Create')%&gt;" onClick="return checkform(document.QuickCreate);" /&gt;&lt;/div&gt;
  &lt;/form&gt;
  &lt;/&amp;&gt;
  &lt;/div&gt;
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 @@
    &lt;input type="submit" &lt;% $Name &amp;&amp; qq[ name="$Name"] | n %&gt; value="&lt;%$AlternateLabel%&gt;" class="button" /&gt;
  % } else {
    &lt;span class="caption"&gt;&lt;%$Caption%&gt;&lt;/span&gt;
-  &lt;input type="submit" &lt;% $Name &amp;&amp; qq[ name="$Name"] | n %&gt; value="&lt;%$Label%&gt;" class="button" /&gt;
+  &lt;input type="submit" &lt;% $Name &amp;&amp; qq[ name="$Name"] | n %&gt; value="&lt;%$Label%&gt;" class="button" &lt;% $JavaScript &amp;&amp; $JavaScript |n %&gt;/&gt;
  % }
    &lt;/div&gt;
    &lt;div class="submit-clear"&gt;&lt;/div&gt;
@@ -81,4 +81,5 @@
  $CheckboxName =&gt; ''
  $Reset =&gt; undef
  $ResetLabel =&gt; loc('Reset')
+$JavaScript =&gt; undef
  &lt;/%ARGS&gt;
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 @@
-&lt;&amp; /Elements/Header, Title=&gt;loc("RT at a glance"), Refresh =&gt; $session{'home_refresh_interval'} &amp;&gt;
+&lt;&amp; /Elements/Header,
+  Title=&gt;loc("RT at a glance"),
+  Refresh =&gt; $session{'home_refresh_interval'},
+  JavaScript =&gt; q(function checkform( form ) {
+    if (form.Subject.value == "") {
+        alert( "You must enter a subject." );
+        form.Subject.focus();
+        return false;
+    }
+    return true;
+} ) &amp;&gt;
  &lt;!--
  % $m-&gt;out('--'.'&gt;');
  % 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 }}}
-&lt;&amp; Elements/Header, Title =&gt; loc("Create a ticket") &amp;&gt;
+&lt;&amp; Elements/Header,
+    Title =&gt; loc("Create a ticket"),
+ JavaScript =&gt; q(function checkform( form ) {
+    if (form.Subject.value == "") {
+        alert( "You must enter a subject." );
+        form.Subject.focus();
+        return false;
+    }
+    return true;
+} ) &amp;&gt;
-&lt;form action="Display.html" method="post" enctype="multipart/form-data"&gt;
+&lt;form action="Display.html" method="post" enctype="multipart/form-data" name="TicketCreate"&gt;
  &lt;input type="hidden" class="hidden" name="id" value="new" /&gt;
  &lt;table&gt;
@@ -102,7 +111,9 @@
  &lt;/td&gt;
  &lt;/tr&gt;
  &lt;/table&gt;
-&lt;&amp; /Elements/Submit, Label =&gt; loc("Create ticket")&amp;&gt;
+&lt;&amp; /Elements/Submit,
+  Label =&gt; loc("Create ticket"),
+  JavaScript =&gt; q(onClick="return checkform(document.TicketCreate);") &amp;&gt;
  &lt;/form&gt;
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 }}}
  &lt;&amp; /Elements/Header,
      Title =&gt; loc("Create a new ticket"),
-    onload =&gt; "hide(document.getElementById('Ticket-Create-details'));" &amp;&gt;
+    onload =&gt; "hide(document.getElementById('Ticket-Create-details'));",
+ JavaScript =&gt; q(function checkform( form ) {
+    if (form.Subject.value == "") {
+        alert( "You must enter a subject." );
+        form.Subject.focus();
+        return false;
+    }
+    return true;
+} ) &amp;&gt;
  &lt;&amp; /Elements/Tabs,
      current_toptab =&gt; "Ticket/Create.html",
      Title =&gt; loc("Create a new ticket"),
@@ -165,7 +173,9 @@
  &lt;/tr&gt;
  &lt;/table&gt;
  &lt;/&amp;&gt;
-&lt;&amp; /Elements/Submit, Label =&gt; loc("Create")&amp;&gt;
+&lt;&amp; /Elements/Submit,
+  Label =&gt; loc("Create"),
+  JavaScript =&gt; q(onClick="return checkform(document.TicketCreate);") &amp;&gt;
  &lt;/div&gt;
  &lt;div id="Ticket-Create-details"&gt;
@@ -232,7 +242,9 @@
  &lt;/td&gt;
  &lt;/tr&gt;
  &lt;/table&gt;
-&lt;&amp; /Elements/Submit, Label =&gt; loc("Create") &amp;&gt;
+&lt;&amp; /Elements/Submit,
+  Label =&gt; loc("Create"),
+  JavaScript =&gt; q(onClick="return checkform(document.TicketCreate);") &amp;&gt;
  &lt;/div&gt;
  &lt;/form&gt;
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 }}}
-&lt;&amp; /Elements/Header, Title =&gt; loc("Ticket #[_1] Jumbo update: [_2]", $Ticket-&gt;Id, $Ticket-&gt;Subject) &amp;&gt;
+&lt;&amp; /Elements/Header,
+  Title =&gt; loc("Ticket #[_1] Jumbo update: [_2]", $Ticket-&gt;Id, $Ticket-&gt;Subject),
+  JavaScript =&gt; q(function checkform( form ) {
+    if (form.Subject.value == "") {
+        alert( "You must enter a subject." );
+        form.Subject.focus();
+        return false;
+    }
+    return true;
+} ) &amp;&gt;
  &lt;&amp; /Ticket/Elements/Tabs,
      Ticket =&gt; $Ticket,
      current_tab =&gt; "Ticket/ModifyAll.html?id=".$Ticket-&gt;Id,
@@ -51,7 +60,7 @@
  &lt;&amp; /Elements/ListActions, actions =&gt; \@results &amp;&gt;
-&lt;form method="post" action="ModifyAll.html" enctype="multipart/form-data"&gt;
+&lt;form method="post" action="ModifyAll.html" enctype="multipart/form-data" name="TicketModify"&gt;
  &lt;&amp; /Elements/Callback, _CallbackName =&gt; 'FormStart',ARGSRef =&gt;\%ARGS &amp;&gt;
  &lt;input type="hidden" class="hidden" name="id" value="&lt;%$Ticket-&gt;Id%&gt;" /&gt;
@@ -125,7 +134,9 @@
  &lt;&amp; /Elements/Submit,
      Label =&gt; loc('Save Changes'),
-    Caption =&gt; loc("If you've updated anything above, be sure to"), color =&gt; "#333399" &amp;&gt;
+    Caption =&gt; loc("If you've updated anything above, be sure to"),
+ color =&gt; "#333399",
+    JavaScript =&gt; q(onClick="return checkform(document.TicketModify);") &amp;&gt;
  &lt;/form&gt;
  &lt;%INIT&gt;
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 }}}
-&lt;&amp; /Elements/Header, Title =&gt; loc('Modify ticket #[_1]', $TicketObj-&gt;Id) &amp;&gt;
+&lt;&amp; /Elements/Header,
+  Title =&gt; loc('Modify ticket #[_1]', $TicketObj-&gt;Id),
+  JavaScript =&gt; q(function checkform( form ) {
+    if (form.Subject.value == "") {
+        alert( "You must enter a subject." );
+        form.Subject.focus();
+        return false;
+    }
+    return true;
+} ) &amp;&gt;
  &lt;&amp; /Ticket/Elements/Tabs,
      Ticket =&gt; $TicketObj, current_subtab =&gt; "Ticket/Modify.html?id=".$TicketObj-&gt;Id,
      Title =&gt; loc('Modify ticket #[_1]', $TicketObj-&gt;Id) &amp;&gt;
  &lt;&amp; /Elements/ListActions, actions =&gt; \@results &amp;&gt;
-&lt;form method="post" action="Modify.html" enctype="multipart/form-data"&gt;
+&lt;form method="post" action="Modify.html" enctype="multipart/form-data" name="TicketModify"&gt;
  &lt;&amp; /Elements/Callback, _CallbackName =&gt; 'FormStart',ARGSRef =&gt;\%ARGS &amp;&gt;
  &lt;input type="hidden" class="hidden" name="id" value="&lt;%$TicketObj-&gt;Id%&gt;" /&gt;
  &lt;&amp;| /Widgets/TitleBox, title =&gt; loc('Modify ticket #[_1]',$TicketObj-&gt;Id) &amp;&gt;
@@ -57,7 +66,11 @@
  &lt;&amp; Elements/EditCustomFields, TicketObj =&gt; $TicketObj &amp;&gt;
  &lt;/&amp;&gt;
-&lt;&amp; /Elements/Submit, Label =&gt; loc('Save Changes'), Caption =&gt; loc("If you've updated anything above, be sure to"), color =&gt; "#993333" &amp;&gt;
+&lt;&amp; /Elements/Submit,
+  Label =&gt; loc('Save Changes'),
+  Caption =&gt; loc("If you've updated anything above, be sure to"),
+  color =&gt; "#993333",
+  JavaScript =&gt; q(onClick="return checkform(document.TicketModify);") &amp;&gt;
  &lt;/form&gt;
  &lt;%INIT&gt;
</nowiki>
 
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 &lt;SCRIPT&gt; 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:
 
&lt;&amp; /Elements/Submit,
    Label =&gt; loc("Create ticket"),
    JavaScript =&gt; q(onClick="return checkform(document.TicketCreate);")
&amp;&gt;
 
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. &lt;form action="Display.html" method="post" enctype="multipart/form-data" name="TicketCreate"&gt;
 
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 =&gt; 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:
 
&lt;input type="submit" class="button" value="&lt;%loc('Create')%&gt;" onClick="return checkform(document.QuickCreate);" /&gt;
 
That's all there is to it! If you have any problems with the patch, please let me know -- [[JoeCasadonte]]

Latest revision as of 16:14, 6 April 2016

Mandatory subjects can be provided by the MandatorySubject extension.