[Bps-public-commit] r17172 - in RT-FormTools: lib/RT/Extension

clkao at bestpractical.com clkao at bestpractical.com
Thu Dec 11 12:52:35 EST 2008


Author: clkao
Date: Thu Dec 11 12:52:35 2008
New Revision: 17172

Modified:
   RT-FormTools/html/FormTools/Form
   RT-FormTools/html/FormTools/ShowChoices
   RT-FormTools/lib/RT/Extension/FormTools.pm

Log:
add cf validation support.


Modified: RT-FormTools/html/FormTools/Form
==============================================================================
--- RT-FormTools/html/FormTools/Form	(original)
+++ RT-FormTools/html/FormTools/Form	Thu Dec 11 12:52:35 2008
@@ -1,6 +1,7 @@
 <%args>
 $next
 $include_header => 1
+$validation => 0
 </%args>
 <%init>
 use RT::Extension::FormTools;
@@ -11,6 +12,30 @@
 
 my %request_args = $m->request_args;
 
+my @results;
+my $real_next = delete $request_args{_form_tools_next};
+if ($validation && $real_next) {
+    my $queue = $m->notes('queue');
+    foreach my $key (keys %request_args) {
+        next if $key =~ /Values-Magic$/;
+        next unless ($key =~ /CustomField-(\d+)/ );
+        my $id = $1;
+        my $cf = RT::CustomField->new($session{'CurrentUser'});
+        $cf->Load($id);
+        next unless exists $request_args{"Object-RT::Ticket--CustomField-@{[ $cf->Id ]}-Values-Magic"};
+        my ($ok, @res) = RT::Extension::FormTools::validate_cf($cf, \%request_args);
+        push @results, @res unless $ok;
+
+    }
+    unless (@results) {
+        my $next_comp = $real_next;
+        $real_next = $m->caller(1)->dir_path . '/' . $real_next
+            unless $real_next =~ m'^/';
+        $m->subexec("$real_next", %ARGS, %request_args);
+        $m->abort;
+    }
+}
+
 foreach my $key (keys %request_args) {
    next unless ($key =~ /CustomField-(\d+)/ );
    my $cf = $1;
@@ -28,9 +53,17 @@
 <& /Elements/PageLayout, title => $m->notes('page_title') &>
 % }
 
-<form method="POST" action="<%$next%>" enctype="multipart/form-data">
+<& /Elements/ListActions, actions => \@results &>
+
+<form method="POST" action="<% $validation ? $m->caller(1)->path : $next %>" enctype="multipart/form-data">
+% if ($validation) {
+<input type="hidden" name="_form_tools_next" value="<%$next%>" />
+% }
 <%$content|n%>
 
+%# XXX: we should only pass CFs and core fields.  this is causing
+%# username/password to be bypassed if we are logining directly on a
+%# formtool page.
 % foreach my $key (keys %request_args) {
 % next if (ref $request_args{$key} && ref $request_args{$key} ne 'ARRAY');
 % foreach my $val ( ref ($request_args{$key}) ? @{$request_args{$key}} : ($request_args{$key})) {

Modified: RT-FormTools/html/FormTools/ShowChoices
==============================================================================
--- RT-FormTools/html/FormTools/ShowChoices	(original)
+++ RT-FormTools/html/FormTools/ShowChoices	Thu Dec 11 12:52:35 2008
@@ -11,7 +11,7 @@
 % my $id = $1;
 %$cf->Load($id);
 <tr><td><b><%$cf->Name%></b></td><td><% ref ($all_fields{$field}) ? join(', ',@{$all_fields{$field}}) : $all_fields{$field}%></td></tr>
-% } else {
+% } elsif (RT::Extension::FormTools::is_core_field($field)) {
 
 <tr><td><b><%$field%></b></td><td><% $all_fields{$field} %></td></tr>
 %}

Modified: RT-FormTools/lib/RT/Extension/FormTools.pm
==============================================================================
--- RT-FormTools/lib/RT/Extension/FormTools.pm	(original)
+++ RT-FormTools/lib/RT/Extension/FormTools.pm	Thu Dec 11 12:52:35 2008
@@ -21,4 +21,48 @@
    return $_[0] =~ /^(Requestors|Cc|AdminCc|Subject|UpdateContent|Attach)$/;
 }
 
+sub validate_cf {
+    my ($CF, $ARGSRef) = @_;
+    my $NamePrefix = "Object-RT::Ticket--CustomField-";
+    my $field = $NamePrefix . $CF->Id . "-Value";
+    my $valid = 1;
+    my $value;
+    my @res;
+    if ($ARGSRef->{"${field}s-Magic"} and exists $ARGSRef->{"${field}s"}) {
+        $value = $ARGSRef->{"${field}s"};
+        # We only validate Single Combos -- multis can never be user input
+        next if ref $value;
+    }
+    else {
+        $value = $ARGSRef->{$field};
+    }
+
+    my @values = ();
+    if ( ref $value eq 'ARRAY' ) {
+        @values = @$value;
+    } elsif ( $CF->Type =~ /text/i ) {
+        @values = ($value);
+    } else {
+        @values = split /\r*\n/, ( defined $value ? $value : '');
+    }
+    @values = grep $_ ne '',
+        map {
+            s/\r+\n/\n/g;
+            s/^\s+//;
+            s/\s+$//;
+            $_;
+        }
+        grep defined, @values;
+    @values = ('') unless @values;
+
+    foreach my $value( @values ) {
+        next if $CF->MatchPattern($value);
+
+        my $msg = "Input must match ". $CF->FriendlyPattern;
+        push @res, $msg;
+        $valid = 0;
+    }
+    return ($valid, @res);
+}
+
 1;



More information about the Bps-public-commit mailing list