rt 4.2.14 (#13852)
[freeside.git] / rt / share / html / Elements / ValidateCustomFields
index 922b885..f04fe2b 100644 (file)
@@ -2,7 +2,7 @@
 %#
 %# COPYRIGHT:
 %#
-%# This software is Copyright (c) 1996-2012 Best Practical Solutions, LLC
+%# This software is Copyright (c) 1996-2017 Best Practical Solutions, LLC
 %#                                          <sales@bestpractical.com>
 %#
 %# (Except where explicitly superseded by other copyright notices)
 <%INIT>
 my ($valid, @res) = (1, ());
 $CustomFields->GotoFirstItem;
+
+my $CFArgs = _ParseObjectCustomFieldArgs( $ARGSRef )->{ref($Object)}{$Object->Id || 0} || {};
+
 while ( my $CF = $CustomFields->Next ) {
-    my $field = $NamePrefix . $CF->Id . "-Value";
+    my $submitted = $CFArgs->{$CF->Id};
+    # Pick the first grouping
+    $submitted = $submitted ? $submitted->{(keys %$submitted)[0]} : {};
 
-    my $value;
-    if ($ARGSRef->{"${field}s-Magic"} and exists $ARGSRef->{"${field}s"}) {
-        $value = $ARGSRef->{"${field}s"};
+    # If we don't have a value and we don't see the Magic, then we're not
+    # submitting a field.
+    next if not $ValidateUnsubmitted
+        and not exists $submitted->{"Value"}
+        and not exists $submitted->{"Upload"}
+        and not exists $submitted->{"Values"}
+        and not $submitted->{"Values-Magic"};
 
-        # We only validate Single Combos -- multis can never be user input
-        next if ref $value;
-    }
-    else {
-        $value = $ARGSRef->{$field};
-    }
-    $m->notes(('Field-' . $CF->Id) => $value);
+    # We only validate Single Combos -- multis can never be user input
+    next if $submitted->{"Values-Magic"} and exists $submitted->{"Values"}
+        and ref $submitted->{"Values"};
 
-    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+$//;
-            $_;
+    $m->notes(('Field-' . $CF->Id) => $submitted->{Values} // $submitted->{Value});
+
+    my @values = _NormalizeObjectCustomFieldValue(
+        CustomField => $CF,
+        Value       => ($submitted->{Values} // $submitted->{Value} // $submitted->{Upload}),
+    );
+    if ($CF->Type =~ /^Date(?:Time)?$/) {
+        if (not @values) {
+            my $values = $Object->CustomFieldValues($CF->Id);
+            while (my $ocfv = $values->Next) {
+                push @values, $ocfv->Content;
+            }
         }
-        grep defined, @values;
-    @values = ('') unless @values;
+        @values = grep {
+            my $DateObj = RT::Date->new ( $session{'CurrentUser'} );
+            $DateObj->Set(
+                Format => 'unknown',
+                Value => $_,
+                ($CF->Type eq "Date" ? (Timezone => 'utc') : ())
+            );
+            $DateObj->IsSet
+        } @values;
+    }
+    push @values, '' unless @values;
 
     for my $value( @values ) {
         if ($value) {
-            if ( $CF->Type eq 'IPAddress' ) {
-                use Regexp::Common qw(RE_net_IPv4);
-                my $ip = RT::ObjectCustomFieldValue->ParseIP( $value );
-                unless ( $ip ) {
-                    my $msg =
-                      loc( "Input can not be parsed as an IP address" );
-                    $m->notes( ( 'InvalidField-' . $CF->Id ) => $msg );
-                    push @res, $msg;
-                    $valid = 0;
-                }
-            }
-            elsif ( $CF->Type eq 'IPAddressRange' ) {
-                my ( $start_ip, $end_ip ) =
-                  RT::ObjectCustomFieldValue->ParseIPRange($value);
-                unless ( $start_ip && $end_ip ) {
-                    my $msg =
-                      loc( "Input can not be parsed as an IP address range" );
-                    $m->notes( ( 'InvalidField-' . $CF->Id ) => $msg );
-                    push @res, $msg;
-                    $valid = 0;
-                }
+            my $ref = { Content => $value };
+            my ($ok, $msg) = $CF->_CanonicalizeValue( $ref );
+            unless ($ok) {
+                $m->notes( ( 'InvalidField-' . $CF->Id ) => $msg );
+                push @res, $CF->Name .': '. $msg;
+                $valid = 0;
             }
         }
 
@@ -111,7 +108,7 @@ while ( my $CF = $CustomFields->Next ) {
 
         my $msg = loc("Input must match [_1]", $CF->FriendlyPattern);
         $m->notes( ('InvalidField-' . $CF->Id) => $msg );
-        push @res, $msg;
+        push @res, $CF->Name .': '. $msg;
         $valid = 0;
     }
 }
@@ -119,7 +116,8 @@ $m->notes('ValidFields', $valid);
 return wantarray? ($valid, @res): $valid;
 </%INIT>
 <%ARGS>
+$Object => RT::Ticket->new( $session{'CurrentUser'})
 $CustomFields
 $ARGSRef
-$NamePrefix => "Object-RT::Ticket--CustomField-"
+$ValidateUnsubmitted => 0
 </%ARGS>