Changeset 1070
- Timestamp:
- 06/12/07 22:47:03 (1 year ago)
- Files:
-
- trunk/Changes (modified) (1 diff)
- trunk/lib/OpenGuides.pm (modified) (4 diffs)
- trunk/lib/OpenGuides/Utils.pm (modified) (1 diff)
- trunk/t/17_commit_node.t (modified) (2 diffs)
- trunk/templates/edit_form.tt (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/Changes
r1065 r1070 19 19 Added new div#nonexistent_node_message for displaying message when 20 20 someone tries to view a nonexistent node. 21 Validate input geodata (#22) 21 22 22 23 0.60 13 May 2007 trunk/lib/OpenGuides.pm
r1069 r1070 460 460 $guide->display_edit_form( 461 461 id => "Vivat Bacchus", 462 vars => \%vars, 463 content => $content, 464 metadata => \%metadata, 465 checksum => $checksum 462 466 ); 463 467 … … 465 469 C<return_output> parameter can be used to return the output instead of 466 470 printing it to STDOUT. 471 472 If this is to redisplay an existing edit, the content, metadata 473 and checksum may be supplied in those arguments 474 475 Extra template variables may be supplied in the vars argument 467 476 468 477 =cut … … 500 509 deter_robots => 1, 501 510 ); 511 512 # Override some things if we were supplied with them 513 $tt_vars{content} = $args{content} if $args{content}; 514 $tt_vars{checksum} = $args{checksum} if $args{checksum}; 515 if (defined $args{vars}) { 516 my %supplied_vars = %{$args{vars}}; 517 foreach my $key ( keys %supplied_vars ) { 518 $tt_vars{$key} = $supplied_vars{$key}; 519 } 520 } 521 if (defined $args{metadata}) { 522 my %supplied_metadata = %{$args{metadata}}; 523 foreach my $key ( keys %supplied_metadata ) { 524 $tt_vars{$key} = $supplied_metadata{$key}; 525 } 526 } 502 527 503 528 my $output = $self->process_template( … … 1341 1366 : 0; 1342 1367 1368 # General validation 1369 my $fails = OpenGuides::Utils->validate_edit( 1370 cgi_obj => $q 1371 ); 1372 1373 if ( scalar @{$fails} ) { 1374 my %vars = ( 1375 validate_failed => $fails 1376 ); 1377 1378 my $output = $self->display_edit_form( 1379 id => $node, 1380 content => CGI->escapeHTML($content), 1381 metadata => \%new_metadata, 1382 vars => \%vars, 1383 checksum => CGI->escapeHTML($checksum), 1384 return_output => 1 1385 ); 1386 1387 return $output if $return_output; 1388 print $output; 1389 return; 1390 } 1391 1343 1392 # If we can, check to see if this edit looks like spam. 1344 1393 my $spam_detector = $config->spam_detector_module; trunk/lib/OpenGuides/Utils.pm
r1069 r1070 356 356 } 357 357 358 =item B<validate_edit> 359 360 my $fails = OpenGuides::Utils->validate_edit( 361 id => $node, 362 cgi_obj => $q 363 ); 364 365 Checks supplied content for general validity. If anything is invalid, 366 returns an array ref of errors to report to the user. 367 368 =cut 369 370 sub validate_edit { 371 my ( $self, %args ) = @_; 372 my $q = $args{cgi_obj}; 373 my @fails; 374 push @fails, "Content missing" unless $q; 375 return \@fails if @fails; 376 377 # Now do our real validation 378 # Numeric tests 379 foreach my $var (qw(os_x os_y)) { 380 if ($q->param($var) and $q->param($var) !~ /^-?\d+$/) { 381 push @fails, "$var must be integer, was: " . $q->param($var); 382 } 383 } 384 385 foreach my $var (qw(latitude longitude)) { 386 if ($q->param($var) and $q->param($var) !~ /^-?\d+\.?(\d+)?$/) { 387 push @fails, "$var must be numeric, was: " . $q->param($var); 388 } 389 } 390 391 return \@fails; 392 393 }; 394 358 395 =back 359 396 trunk/t/17_commit_node.t
r956 r1070 19 19 20 20 21 plan tests => 5;21 plan tests => 7; 22 22 23 23 # Clear out the database from any previous runs. … … 98 98 is( $node{version}, 2, "First version" ); 99 99 is( $node{metadata}->{edit_type}[0], "Normal edit", "Right edit type" ); 100 101 # Now try to commit some invalid data, and make sure we get an edit form back 102 $q = CGI->new; 103 $q->param( -name => "content", -value => "foo" ); 104 $q->param( -name => "categories", -value => "" ); 105 $q->param( -name => "locales", -value => "" ); 106 $q->param( -name => "phone", -value => "" ); 107 $q->param( -name => "fax", -value => "" ); 108 $q->param( -name => "website", -value => "" ); 109 $q->param( -name => "hours_text", -value => "" ); 110 $q->param( -name => "address", -value => "" ); 111 $q->param( -name => "postcode", -value => "" ); 112 $q->param( -name => "map_link", -value => "" ); 113 $q->param( -name => "os_x", -value => "fooooo" ); 114 $q->param( -name => "os_y", -value => "" ); 115 $q->param( -name => "username", -value => "bob" ); 116 $q->param( -name => "comment", -value => "foo" ); 117 $q->param( -name => "node_image", -value => "image" ); 118 $q->param( -name => "edit_type", -value => "Minor tidying" ); 119 120 $output = $guide->commit_node( 121 return_output => 1, 122 id => "Wombats again", 123 cgi_obj => $q, 124 ); 125 126 like( $output, qr/Your input was invalid/, 127 "Edit form displayed and invalid input message shown if invalid input" ); 128 129 like( $output, qr/os_x must be integer/, 130 "Edit form displayed and os_x integer message displayed" ); trunk/templates/edit_form.tt
r1053 r1070 10 10 You will need to manually merge your changes into the new version. 11 11 </div> 12 [% END %] 13 14 [% IF validate_failed %] 15 <div class="warning_text"> 16 <p> 17 Your input was invalid: 18 </p> 19 <ul> 20 [% FOREACH error = validate_failed %] 21 <li>[% error %]</li> 22 [% END %] 23 </ul> 24 <p> 25 Please correct your input data and try again. 26 </p> 12 27 [% END %] 13 28
