| 1 | # | ||
| 2 | # Results.pm - Object which contains validation result. | ||
| 3 | # | ||
| 4 | # This file is part of FormValidator. | ||
| 5 | # | ||
| 6 | # Author: Francis J. Lacoste <francis.lacoste@iNsu.COM> | ||
| 7 | # | ||
| 8 | # Copyright (C) 2000 iNsu Innovations Inc. | ||
| 9 | # | ||
| 10 | # This program is free software; you can redistribute it and/or modify | ||
| 11 | # it under the terms same terms as perl itself. | ||
| 12 | # | ||
| 13 | use strict; | ||
| 14 | |||
| 15 | package HTML::FormValidator::Results; | ||
| 16 | |||
| 17 | use HTML::FormValidator::Constraints; | ||
| 18 | use HTML::FormValidator::Filters; | ||
| 19 | |||
| 20 | =pod | ||
| 21 | |||
| 22 | =head1 NAME | ||
| 23 | |||
| 24 | HTML::FormValidator::Results - Object which contains the results of an input validation. | ||
| 25 | |||
| 26 | =head1 SYNOPSIS | ||
| 27 | |||
| 28 | my $results = $validator->check( \%fdat, "customer_infos" ); | ||
| 29 | |||
| 30 | # Print the name of missing fields | ||
| 31 | if ( $results->has_missing ) { | ||
| 32 | foreach my $f ( $results->missing ) { | ||
| 33 | print $f, " is missing\n"; | ||
| 34 | } | ||
| 35 | } | ||
| 36 | |||
| 37 | # Print the name of invalid fields | ||
| 38 | if ( $results->has_invalid ) { | ||
| 39 | foreach my $f ( $results->invalid ) { | ||
| 40 | print $f, " is invalid: ", $results->invalid( $f ) \n"; | ||
| 41 | } | ||
| 42 | } | ||
| 43 | |||
| 44 | # Print fields with warnings | ||
| 45 | if ( $results->has_warnings ) { | ||
| 46 | foreach my $f ( $results->warnings ) { | ||
| 47 | print $f, "'s value is not recommended\n"; | ||
| 48 | } | ||
| 49 | } | ||
| 50 | |||
| 51 | # Print unknown fields | ||
| 52 | if ( $results->has_unknown ) { | ||
| 53 | foreach my $f ( $results->unknown ) { | ||
| 54 | print $f, " is unknown\n"; | ||
| 55 | } | ||
| 56 | } | ||
| 57 | |||
| 58 | # Print conflicting fields | ||
| 59 | if ( $results->has_conflicts ) { | ||
| 60 | foreach my $f ( $results->conflicts ) { | ||
| 61 | print $f, " conflicts with ", join( "," $results->conflicts( $f)), "\n"; | ||
| 62 | } | ||
| 63 | } | ||
| 64 | |||
| 65 | # Print valid fields | ||
| 66 | foreach my $f ( $results->valid() ) { | ||
| 67 | print $f, " = ", $result->valid( $f ), "\n"; | ||
| 68 | } | ||
| 69 | |||
| 70 | =head1 DESCRIPTION | ||
| 71 | |||
| 72 | This is the object returned by the HTML::FormValidator check method. It can | ||
| 73 | be queried for information about the validation results. | ||
| 74 | |||
| 75 | =cut | ||
| 76 | |||
| 77 | sub new { | ||
| 78 | my $proto = shift; | ||
| 79 | my $class = ref $proto || $proto; | ||
| 80 | |||
| 81 | my ($profile, $data) = @_; | ||
| 82 | |||
| 83 | my $self = bless {}, $class; | ||
| 84 | |||
| 85 | $self->_process( $profile, $data ); | ||
| 86 | |||
| 87 | $self; | ||
| 88 | } | ||
| 89 | |||
| 90 | sub _process { | ||
| 91 | my ($self, $profile, $data) = @_; | ||
| 92 | |||
| 93 | # Copy data and assumes that all is valid | ||
| 94 | my %valid = %$data; | ||
| 95 | my %missing; | ||
| 96 | my %invalid; | ||
| 97 | my %unknown; | ||
| 98 | my %conflicts; | ||
| 99 | my %warnings; | ||
| 100 | |||
| 101 | # Apply inconditional filters | ||
| 102 | foreach my $filter ( @{$profile->{filters}} ) { | ||
| 103 | # Qualify symbolic references | ||
| 104 | $filter = ref $filter ? $filter : | ||
| 105 | "HTML::FormValidator::Filters::filter_" . $filter; | ||
| 106 | foreach my $field ( keys %valid ) { | ||
| 107 | no strict 'refs'; | ||
| 108 | $valid{$field} = $filter->( $valid{$field} ); | ||
| 109 | } | ||
| 110 | } | ||
| 111 | |||
| 112 | # Apply specific filters | ||
| 113 | while ( my ($field,$filters) = each %{$profile->{field_filters} }) { | ||
| 114 | my @f = ref $filters eq "ARRAY" ? @$filters : ( $filters ); | ||
| 115 | my $v = $valid{$field}; | ||
| 116 | foreach my $filter ( @f ) { | ||
| 117 | # Qualify symbolic references | ||
| 118 | $filter = ref $filter ? $filter : | ||
| 119 | "HTML::FormValidator::Filters::filter_" . $filter; | ||
| 120 | no strict 'refs'; | ||
| 121 | |||
| 122 | $v = $filter->( $v ); | ||
| 123 | } | ||
| 124 | $valid{$field} = $v; | ||
| 125 | } | ||
| 126 | |||
| 127 | # Remove all empty fields | ||
| 128 | foreach my $field ( keys %valid ) { | ||
| 129 | delete $valid{$field} unless length $valid{$field}; | ||
| 130 | } | ||
| 131 | |||
| 132 | my %required = map { $_ => 1 } @{$profile->{required}}; | ||
| 133 | my %optional = map { $_ => 1 } @{$profile->{optional}}; | ||
| 134 | |||
| 135 | # Check if the presence of some fields makes other optional | ||
| 136 | # fields required. | ||
| 137 | while ( my ( $field, $deps) = each %{$profile->{dependencies}} ) { | ||
| 138 | if ( $valid{$field} ) { | ||
| 139 | foreach my $dep ( @$deps ) { | ||
| 140 | $required{$dep} = 1; | ||
| 141 | } | ||
| 142 | } | ||
| 143 | } | ||
| 144 | |||
| 145 | # Find unknown | ||
| 146 | %unknown = map { $_ => $valid{$_} } | ||
| 147 | grep { ! (exists $optional{$_} || exists $required{$_} ) } keys %valid; | ||
| 148 | # and remove them from the list | ||
| 149 | foreach my $field ( keys %unknown ) { | ||
| 150 | delete $valid{$field}; | ||
| 151 | } | ||
| 152 | |||
| 153 | # Fill defaults | ||
| 154 | while ( my ($field,$value) = each %{$profile->{defaults}} ) { | ||
| 155 | $valid{$field} = $value unless exists $valid{$field}; | ||
| 156 | } | ||
| 157 | |||
| 158 | # Check for required fields | ||
| 159 | foreach my $field ( keys %required ) { | ||
| 160 | $missing{$field} = 1 unless exists $valid{$field}; | ||
| 161 | } | ||
| 162 | |||
| 163 | # Find conflicts | ||
| 164 | while ( my ( $field, $conflict ) = each %{$profile->{conflicts}} ) { | ||
| 165 | foreach my $c ( @$conflict ) { | ||
| 166 | if ( exists $valid{$c} ) { | ||
| 167 | push @{$conflicts{$c}}, $field; | ||
| 168 | delete $valid{$c}; | ||
| 169 | } | ||
| 170 | } | ||
| 171 | } | ||
| 172 | |||
| 173 | # Check constraints | ||
| 174 | while ( my ($field,$constraint_spec) = each %{$profile->{constraints}} ) { | ||
| 175 | my ($constraint,@params); | ||
| 176 | if ( ref $constraint_spec eq "HASH" ) { | ||
| 177 | $constraint = $constraint_spec->{constraint}; | ||
| 178 | foreach my $fname ( @{$constraint_spec->{params} } ) { | ||
| 179 | push @params, $valid{$fname}; | ||
| 180 | } | ||
| 181 | } else { | ||
| 182 | $constraint = $constraint_spec; | ||
| 183 | @params = ( $valid{$field} ); | ||
| 184 | } | ||
| 185 | next unless exists $valid{$field}; | ||
| 186 | |||
| 187 | unless ( ref $constraint ) { | ||
| 188 | # Check for regexp constraint | ||
| 189 | if ( $constraint =~ m@^\s*(/.+/|m(.).+\2)[cgimosx]*\s*$@ ) { | ||
| 190 | my $sub = eval 'sub { $_[0] =~ '. $constraint . '}'; | ||
| 191 | die "Error compiling regular expression $constraint: $@" if $@; | ||
| 192 | $constraint = $sub; | ||
| 193 | # Cache for next use | ||
| 194 | if ( ref $constraint_spec eq "HASH" ) { | ||
| 195 | $constraint_spec->{constraint} = $sub; | ||
| 196 | } else { | ||
| 197 | $profile->{constraints}{$field} = $sub; | ||
| 198 | } | ||
| 199 | } else { | ||
| 200 | # Qualify symbolic reference | ||
| 201 | $constraint = "HTML::FormValidator::Constraints::valid_" . | ||
| 202 | $constraint; | ||
| 203 | } | ||
| 204 | } | ||
| 205 | no strict 'refs'; | ||
| 206 | |||
| 207 | my $r = $constraint->( @params ); | ||
| 208 | if ( $r == -1 ) { | ||
| 209 | $warnings{$field} = 1; | ||
| 210 | } elsif ( ! $r ) { | ||
| 211 | $invalid{$field} = $valid{$field}; | ||
| 212 | delete $valid{$field}; | ||
| 213 | } | ||
| 214 | } | ||
| 215 | |||
| 216 | $self->{valid} = \%valid; | ||
| 217 | $self->{invalid} = \%invalid; | ||
| 218 | $self->{unknown} = \%unknown; | ||
| 219 | $self->{missing} = \%missing; | ||
| 220 | $self->{conflicts} = \%conflicts; | ||
| 221 | $self->{warnings} = \%warnings; | ||
| 222 | } | ||
| 223 | |||
| 224 | =pod | ||
| 225 | |||
| 226 | =head1 valid( [field] ) | ||
| 227 | |||
| 228 | This method returns in an array context the list of fields which | ||
| 229 | contains valid value. In a scalar context, it returns an hash reference | ||
| 230 | which contains the valid fields and their value. | ||
| 231 | |||
| 232 | If called with an argument, it returns the value of that field if it | ||
| 233 | contains valid data, undef otherwise. | ||
| 234 | |||
| 235 | =cut | ||
| 236 | |||
| 237 | sub valid { | ||
| 238 | return $_[0]{valid}{$_[1]} if (defined $_[1]); | ||
| 239 | |||
| 240 | wantarray ? keys %{$_[0]{valid}} : $_[0]{valid}; | ||
| 241 | } | ||
| 242 | |||
| 243 | =pod | ||
| 244 | |||
| 245 | =head1 has_missing() | ||
| 246 | |||
| 247 | This method returns true if the results contains missing fields. | ||
| 248 | |||
| 249 | =cut | ||
| 250 | |||
| 251 | sub has_missing { | ||
| 252 | return scalar keys %{$_[0]{missing}}; | ||
| 253 | } | ||
| 254 | |||
| 255 | =pod | ||
| 256 | |||
| 257 | =head1 missing( [field] ) | ||
| 258 | |||
| 259 | This method returns in an array context the list of fields which | ||
| 260 | are missing. In a scalar context, it returns an array reference | ||
| 261 | to the list of missing fields. | ||
| 262 | |||
| 263 | If called with an argument, it returns true if that field is missing, | ||
| 264 | undef otherwise. | ||
| 265 | |||
| 266 | =cut | ||
| 267 | |||
| 268 | sub missing { | ||
| 269 | return $_[0]{missing}{$_[1]} if (defined $_[1]); | ||
| 270 | |||
| 271 | wantarray ? keys %{$_[0]{missing}} : [ keys %{$_[0]{missing}} ]; | ||
| 272 | } | ||
| 273 | |||
| 274 | =pod | ||
| 275 | |||
| 276 | =head1 has_invalid() | ||
| 277 | |||
| 278 | This method returns true if the results contains fields with invalid | ||
| 279 | data. | ||
| 280 | |||
| 281 | =cut | ||
| 282 | |||
| 283 | sub has_invalid { | ||
| 284 | return scalar keys %{$_[0]{invalid}}; | ||
| 285 | } | ||
| 286 | |||
| 287 | =pod | ||
| 288 | |||
| 289 | =head1 invalid( [field] ) | ||
| 290 | |||
| 291 | This method returns in an array context the list of fields which | ||
| 292 | contains invalid value. In a scalar context, it returns an hash reference | ||
| 293 | which contains the invalid fields and their value. | ||
| 294 | |||
| 295 | If called with an argument, it returns the value of that field if it | ||
| 296 | contains invalid data, undef otherwise. | ||
| 297 | |||
| 298 | =cut | ||
| 299 | |||
| 300 | sub invalid { | ||
| 301 | return $_[0]{invalid}{$_[1]} if (defined $_[1]); | ||
| 302 | |||
| 303 | wantarray ? keys %{$_[0]{invalid}} : $_[0]{invalid}; | ||
| 304 | } | ||
| 305 | |||
| 306 | =pod | ||
| 307 | |||
| 308 | =head1 has_unknown() | ||
| 309 | |||
| 310 | This method returns true if the results contains unknown fields. | ||
| 311 | |||
| 312 | =cut | ||
| 313 | |||
| 314 | sub has_unknown { | ||
| 315 | return scalar keys %{$_[0]{unknown}}; | ||
| 316 | |||
| 317 | } | ||
| 318 | |||
| 319 | =pod | ||
| 320 | |||
| 321 | =head1 unknown( [field] ) | ||
| 322 | |||
| 323 | This method returns in an array context the list of fields which | ||
| 324 | are unknown. In a scalar context, it returns an hash reference | ||
| 325 | which contains the unknown fields and their value. | ||
| 326 | |||
| 327 | If called with an argument, it returns the value of that field if it | ||
| 328 | is unknown, undef otherwise. | ||
| 329 | |||
| 330 | =cut | ||
| 331 | |||
| 332 | sub unknown { | ||
| 333 | return $_[0]{unknown}{$_[1]} if (defined $_[1]); | ||
| 334 | |||
| 335 | wantarray ? keys %{$_[0]{unknown}} : $_[0]{unknown}; | ||
| 336 | } | ||
| 337 | |||
| 338 | =pod | ||
| 339 | |||
| 340 | =head1 has_warnings() | ||
| 341 | |||
| 342 | This method returns true if the results contains conflicts. | ||
| 343 | |||
| 344 | =cut | ||
| 345 | |||
| 346 | sub has_warnings { | ||
| 347 | return scalar keys %{$_[0]{warnings}}; | ||
| 348 | } | ||
| 349 | |||
| 350 | =pod | ||
| 351 | |||
| 352 | =head1 warnings( [field] ) | ||
| 353 | |||
| 354 | This method returns in an array context the list of fields which | ||
| 355 | have warnings (a constraint returned -1). In a scalar context, | ||
| 356 | it returns an array reference to the list of fields which have | ||
| 357 | warnings. | ||
| 358 | |||
| 359 | If called with an argument, it returns true if that field has a | ||
| 360 | warning associated with it. | ||
| 361 | |||
| 362 | =cut | ||
| 363 | |||
| 364 | sub warnings { | ||
| 365 | return $_[0]{warnings}{$_[1]} if (defined $_[1]); | ||
| 366 | |||
| 367 | wantarray ? keys %{$_[0]{warnings}} : $_[0]{warnings}; | ||
| 368 | } | ||
| 369 | |||
| 370 | =pod | ||
| 371 | |||
| 372 | =head1 has_conflicts() | ||
| 373 | |||
| 374 | Return true if some fields have conflicts. | ||
| 375 | |||
| 376 | =cut | ||
| 377 | |||
| 378 | sub has_conflicts { | ||
| 379 | return scalar keys %{$_[0]{conflicts}}; | ||
| 380 | } | ||
| 381 | |||
| 382 | =pod | ||
| 383 | |||
| 384 | =head1 conflicts( [field] ) | ||
| 385 | |||
| 386 | This method returns in an array context the list of fields which | ||
| 387 | contains have conflicts. In a scalar context, it returns an hash reference | ||
| 388 | which contains the fields and their conflicts. | ||
| 389 | |||
| 390 | If called with an argument, it returns in an array context the list of | ||
| 391 | conflicts for that field, or an array reference to the conflicts list in a | ||
| 392 | scalar context. | ||
| 393 | |||
| 394 | =cut | ||
| 395 | |||
| 396 | sub conflicts { | ||
| 397 | if ( @_ == 2 ) { | ||
| 398 | if ( wantarray ) { | ||
| 399 | my $ref = $_[0]{conflicts}{$_[1]}; | ||
| 400 | return defined $ref ? @$ref : $ref; | ||
| 401 | } else { | ||
| 402 | return $_[0]{conflicts}{$_[1]}; | ||
| 403 | } | ||
| 404 | } | ||
| 405 | |||
| 406 | wantarray ? keys %{$_[0]{conflicts}} : [ keys %{$_[0]{conflicts}} ]; | ||
| 407 | } | ||
| 408 | |||
| 409 | 1; | ||
| 410 | |||
| 411 | __END__ |