| 1 | package CGI::QuickForm ; # Documented at the __END__. | ||
| 2 | |||
| 3 | # $Id: QuickForm.pm,v 1.71 2002/04/30 11:20:03 mark Exp mark $ | ||
| 4 | |||
| 5 | require 5.004 ; | ||
| 6 | |||
| 7 | 1 | ▶ | use strict ; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | |||
| 9 | 0 | ▶ | use CGI qw( :standard :html3 ) ; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 10 | #use CGI::Carp qw( fatalsToBrowser ) ; | ||
| 11 | |||
| 12 | use vars qw( | ||
| 13 | $VERSION @ISA @EXPORT @EXPORT_OK | ||
| 14 | %Translate | ||
| 15 | ) ; | ||
| 16 | |||
| 17 | $VERSION = '1.93' ; | ||
| 18 | |||
| 19 | use Exporter() ; | ||
| 20 | |||
| 21 | @ISA = qw( Exporter ) ; | ||
| 22 | |||
| 23 | @EXPORT = qw( show_form ) ; | ||
| 24 | |||
| 25 | |||
| 26 | # &colour is not documented because at some point it may be moved elsewhere. | ||
| 27 | @EXPORT_OK = qw( colour color ) ; | ||
| 28 | *color = \&colour ; | ||
| 29 | sub colour { qq{<span style="color:$_[0]">$_[1]</span>} } | ||
| 30 | |||
| 31 | |||
| 32 | sub show_form { | ||
| 33 | my %form = ( | ||
| 34 | -LANGUAGE => 'en', # Language to use for default messages | ||
| 35 | -USER_REQUIRED => undef, | ||
| 36 | -USER_INVALID => undef, | ||
| 37 | -REQUIRED_HTML => '<span style="font-weight:bold;color:BLUE">+</span>', | ||
| 38 | -INVALID_HTML => '<span style="font-weight:bold;color:RED">*</span>', | ||
| 39 | -TITLE => 'Quick Form', # Default page title and heading | ||
| 40 | -INTRO => undef, | ||
| 41 | -HEADER => undef, | ||
| 42 | -FOOTER => undef, | ||
| 43 | -NAME => '', | ||
| 44 | -JSCRIPT => {}, | ||
| 45 | -ONSUBMIT => '', | ||
| 46 | -ACCEPT => \&_on_valid_form, | ||
| 47 | -VALIDATE => undef, # Call this to validate entire form | ||
| 48 | -SIZE => undef, | ||
| 49 | -MAXLENGTH => undef, | ||
| 50 | -ROWS => undef, | ||
| 51 | -COLUMNS => undef, | ||
| 52 | -BORDER => 0, | ||
| 53 | -CHECK => 1, | ||
| 54 | -SPACE => 0, | ||
| 55 | -MULTI_COLUMN => 0, | ||
| 56 | -STYLE_FIELDNAME => '', | ||
| 57 | -STYLE_FIELDVALUE => '', | ||
| 58 | -STYLE_BUTTONS => '', | ||
| 59 | -STYLE_ROW => '', | ||
| 60 | -STYLE_DESC => '', | ||
| 61 | -STYLE_WHY => '', | ||
| 62 | -TABLE_OPTIONS => '', | ||
| 63 | -FIELDS => [ { -LABEL => 'No Fields Specified' } ], | ||
| 64 | -BUTTONS => [ { -name => 'Submit' } ], # Default button | ||
| 65 | @_, | ||
| 66 | ) ; | ||
| 67 | |||
| 68 | # Backward compatibility. | ||
| 69 | $form{-LANGUAGE} = 'en' if $form{-LANGUAGE} eq 'english' ; | ||
| 70 | $form{-BUTTONS}[0]{-name} = $form{-BUTTONLABEL} if $form{-BUTTONLABEL} ; | ||
| 71 | |||
| 72 | if( $form{-LANGUAGE} eq 'user' ) { | ||
| 73 | $Translate{'user'}{-REQUIRED} = | ||
| 74 | $form{-USER_REQUIRED} || $Translate{'en'}{-REQUIRED} ; | ||
| 75 | $Translate{'user'}{-INVALID} = | ||
| 76 | $form{-USER_INVALID} || $Translate{'en'}{-INVALID} ; | ||
| 77 | } | ||
| 78 | $Translate{$form{-LANGUAGE}}{-REQUIRED} =~ s/~R~/$form{-REQUIRED_HTML}/go ; | ||
| 79 | $Translate{$form{-LANGUAGE}}{-INVALID} =~ s/~I~/$form{-INVALID_HTML}/go ; | ||
| 80 | |||
| 81 | $form{-REQUIRED} = 0 ; # Assume no fields are required. | ||
| 82 | |||
| 83 | foreach my $style ( qw( FIELDNAME FIELDVALUE BUTTONS DESC WHY ) ) { | ||
| 84 | $form{"-STYLE_$style"} = qq{ $form{"-STYLE_$style"}} | ||
| 85 | if $form{"-STYLE_$style"} ; | ||
| 86 | } | ||
| 87 | $form{"-STYLE_BUTTONS"} = 'center' | ||
| 88 | if $form{"-STYLE_BUTTONS"} =~ /^ CENT(?:ER|RE)$/oi ; | ||
| 89 | |||
| 90 | $form{-TABLE_OPTIONS} = " $form{-TABLE_OPTIONS}" if $form{-TABLE_OPTIONS} ; | ||
| 91 | $form{-MULTIPART} = 0; # Assume single part forms | ||
| 92 | |||
| 93 | my $i = 0 ; | ||
| 94 | foreach my $fieldref ( @{$form{-FIELDS}} ) { | ||
| 95 | # We have to write back to the original data, $fieldref only points to | ||
| 96 | # a copy. | ||
| 97 | foreach my $style ( qw( ROW FIELDNAME FIELDVALUE DESC ) ) { | ||
| 98 | my $value = $form{-FIELDS}[$i]{"-STYLE_$style"} || $form{"-STYLE_$style"} ; | ||
| 99 | $form{-FIELDS}[$i]{"-STYLE_$style"} = $value ? " $value" : '' ; | ||
| 100 | } | ||
| 101 | $form{-FIELDS}[$i]{-STYLE_FIELDVALUE} .= | ||
| 102 | qq{ colspan="$form{-FIELDS}[$i]{-COLSPAN}"} | ||
| 103 | if $form{-FIELDS}[$i]{-COLSPAN} and $form{-MULTI_COLUMN} ; #" | ||
| 104 | |||
| 105 | $form{-FIELDS}[$i]{-LABEL} = $fieldref->{-name} unless $fieldref->{-LABEL} ; | ||
| 106 | $form{-FIELDS}[$i]{-name} = $fieldref->{-LABEL} unless $fieldref->{-name} ; | ||
| 107 | $form{-FIELDS}[$i]{-TYPE} = 'textfield' unless $fieldref->{-TYPE} ; | ||
| 108 | $form{-MULTIPART} = 1 if $form{-FIELDS}[$i]{-TYPE} eq 'filefield' ; | ||
| 109 | |||
| 110 | $form{-FIELDS}[$i]{-START_ROW} = 1 | ||
| 111 | if $i == 0 or $form{-FIELDS}[$i - 1]{-END_ROW} or not $form{-MULTI_COLUMN} ; | ||
| 112 | |||
| 113 | $form{-FIELDS}[$i]{-END_ROW} = 1 | ||
| 114 | if $i == $#{$form{-FIELDS}} or not $form{-MULTI_COLUMN} ; | ||
| 115 | |||
| 116 | $form{-REQUIRED} = 1 if $fieldref->{-REQUIRED} ; | ||
| 117 | if( $form{-FIELDS}[$i]{-TYPE} eq 'textfield' ) { | ||
| 118 | if( $form{-SIZE} and not $fieldref->{-size} ) { | ||
| 119 | $form{-FIELDS}[$i]{-size} = $form{-SIZE} ; | ||
| 120 | } | ||
| 121 | if( $form{-MAXLENGTH} and not $fieldref->{-maxlength} ) { | ||
| 122 | $form{-FIELDS}[$i]{-maxlength} = $form{-MAXLENGTH} ; | ||
| 123 | } | ||
| 124 | } | ||
| 125 | elsif( $form{-FIELDS}[$i]{-TYPE} eq 'textarea' ) { | ||
| 126 | if( $form{-ROWS} and not $fieldref->{-rows} ) { | ||
| 127 | $form{-FIELDS}[$i]{-rows} = $form{-ROWS} ; | ||
| 128 | } | ||
| 129 | if( $form{-COLUMNS} and not $fieldref->{-columns} ) { | ||
| 130 | $form{-FIELDS}[$i]{-columns} = $form{-COLUMNS} ; | ||
| 131 | } | ||
| 132 | } | ||
| 133 | $i++ ; | ||
| 134 | } | ||
| 135 | |||
| 136 | if( $form{-CHECK} and param() ) { | ||
| 137 | &_check_form( \%form ) ; | ||
| 138 | } | ||
| 139 | else { | ||
| 140 | &_show_form( \%form ) ; | ||
| 141 | } | ||
| 142 | } | ||
| 143 | |||
| 144 | |||
| 145 | sub _check_form { | ||
| 146 | my $formref = shift ; | ||
| 147 | |||
| 148 | $formref->{-INVALID} = 0 ; | ||
| 149 | my %field ; | ||
| 150 | |||
| 151 | my $i = 0 ; | ||
| 152 | foreach my $fieldref ( @{$formref->{-FIELDS}} ) { | ||
| 153 | # We have to write back to the original data, $fieldref only points to | ||
| 154 | # a copy. | ||
| 155 | my( $valid, $why ) = | ||
| 156 | defined $fieldref->{-VALIDATE} ? | ||
| 157 | &{$fieldref->{-VALIDATE}}( param( $fieldref->{-name} ) ) : | ||
| 158 | ( 1, '' ) ; | ||
| 159 | $formref->{-FIELDS}[$i]{-INVALID} = 1, | ||
| 160 | |||
| 161 | $formref->{-FIELDS}[$i]{-WHY} = $valid ? | ||
| 162 | undef : "<span$formref->{-STYLE_WHY}>$why</span>", | ||
| 163 | |||
| 164 | $formref->{-INVALID}++ | ||
| 165 | if ( $fieldref->{-REQUIRED} and not param( $fieldref->{-name} ) ) or | ||
| 166 | not $valid ; | ||
| 167 | |||
| 168 | $field{$fieldref->{-name}} = param( $fieldref->{-name} ) ; | ||
| 169 | $i++ ; | ||
| 170 | } | ||
| 171 | |||
| 172 | if( not $formref->{-INVALID} and defined $formref->{-VALIDATE} ) { | ||
| 173 | # If all the individual parts are valid, check that the record as a | ||
| 174 | # whole is valid. The parameters are presented in a name=>value hash. | ||
| 175 | my( $valid, $why ) = &{$formref->{-VALIDATE}}( %field ) ; | ||
| 176 | $formref->{-INVALID} = not $valid ; | ||
| 177 | $formref->{-WHY} = $why ; | ||
| 178 | } | ||
| 179 | |||
| 180 | if( $formref->{-INVALID} ) { | ||
| 181 | &_show_form( $formref ) ; | ||
| 182 | } | ||
| 183 | else { | ||
| 184 | # Clean any fields that have a clean routine specified. | ||
| 185 | foreach my $fieldref ( @{$formref->{-FIELDS}} ) { | ||
| 186 | param( $fieldref->{-name}, | ||
| 187 | &{$fieldref->{-CLEAN}}( param( $fieldref->{-name} ) ) ) | ||
| 188 | if defined $fieldref->{-CLEAN} ; | ||
| 189 | } | ||
| 190 | &{$formref->{-ACCEPT}} ; | ||
| 191 | } | ||
| 192 | } | ||
| 193 | |||
| 194 | |||
| 195 | sub _show_form { | ||
| 196 | my $formref = shift ; | ||
| 197 | |||
| 198 | my $invalid = delete $formref->{-INVALID} ; | ||
| 199 | my $why = delete $formref->{-WHY} ; | ||
| 200 | my $n = delete $formref->{-SPACE} ? "\n" : '' ; | ||
| 201 | |||
| 202 | if( $formref->{-HEADER} ) { | ||
| 203 | if ( ref $formref->{-HEADER} eq 'CODE' ) { | ||
| 204 | &{$formref->{-HEADER}} ; | ||
| 205 | } | ||
| 206 | else { | ||
| 207 | print "$formref->{-HEADER}$n" ; | ||
| 208 | } | ||
| 209 | } | ||
| 210 | else { | ||
| 211 | |||
| 212 | header, | ||
| 213 | start_html( | ||
| 214 | -title => $formref->{-TITLE}, | ||
| 215 | -script => $formref->{-JSCRIPT} ), $n, | ||
| 216 | h3( $formref->{-TITLE} ), $n, | ||
| 217 | p( $formref->{-INTRO} || $Translate{$formref->{-LANGUAGE}}{-INTRO} ), $n, | ||
| 218 | ; | ||
| 219 | } | ||
| 220 | |||
| 221 | print "<span$formref->{-STYLE_WHY}>$why</span><br />$n" | ||
| 222 | if $invalid and defined $why ; | ||
| 223 | print "$Translate{$formref->{-LANGUAGE}}{-REQUIRED}$n" | ||
| 224 | if $formref->{-REQUIRED} ; | ||
| 225 | print " $Translate{$formref->{-LANGUAGE}}{-INVALID}$n" | ||
| 226 | if $invalid and not defined $why ; | ||
| 227 | |||
| 228 | my $start_form = $formref->{-MULTIPART} ? | ||
| 229 | \&CGI::start_multipart_form : \&CGI::start_form ; | ||
| 230 | if( defined( $ENV{'GATEWAY_INTERFACE'} ) and | ||
| 231 | ( $ENV{'GATEWAY_INTERFACE'} =~ /^CGI-Perl/ ) ) { | ||
| 232 | print &$start_form( | ||
| 233 | -name => $formref->{-NAME}, | ||
| 234 | -onSubmit => $formref->{-ONSUBMIT}, | ||
| 235 | -action => ( script_name() || '' ) . ( path_info() || '' ) | ||
| 236 | ), $n ; | ||
| 237 | } | ||
| 238 | else { | ||
| 239 | print &$start_form( | ||
| 240 | -name => $formref->{-NAME}, | ||
| 241 | -onSubmit => $formref->{-ONSUBMIT} | ||
| 242 | ), $n ; | ||
| 243 | } | ||
| 244 | |||
| 245 | print qq{$n<table border="$formref->{-BORDER}"$formref->{-TABLE_OPTIONS}>$n} ; #" | ||
| 246 | |||
| 247 | my @hidden ; | ||
| 248 | |||
| 249 | foreach my $fieldref ( @{$formref->{-FIELDS}} ) { | ||
| 250 | my %field = %$fieldref ; | ||
| 251 | my $type = delete $field{-TYPE} ; | ||
| 252 | push @hidden, $fieldref if $type eq 'hidden' ; | ||
| 253 | next if $type eq 'submit' or $type eq 'hidden' ; | ||
| 254 | my $required = delete $field{-REQUIRED} ; | ||
| 255 | $required = $required ? $formref->{-REQUIRED_HTML} : '' ; | ||
| 256 | my $invalid = delete $field{-INVALID} ; | ||
| 257 | $invalid = $invalid ? $formref->{-INVALID_HTML} : '' ; | ||
| 258 | my $why = delete $field{-WHY} ; | ||
| 259 | my $rowstyle = delete $field{-STYLE_ROW} ; | ||
| 260 | my $namestyle = delete $field{-STYLE_FIELDNAME} ; | ||
| 261 | my $valuestyle = delete $field{-STYLE_FIELDVALUE} ; | ||
| 262 | my $descstyle = delete $field{-STYLE_DESC} ; | ||
| 263 | my $endrow = delete $field{-END_ROW} ; | ||
| 264 | if( $field{-HEADLINE} ) { | ||
| 265 | $field{-COLSPAN} ||= 2 ; | ||
| 266 | $namestyle .= qq{ colspan="$field{-COLSPAN}"} ; #" | ||
| 267 | } | ||
| 268 | print qq{<tr$rowstyle>$n} if delete $field{-START_ROW} ; | ||
| 269 | print qq{<td$namestyle>$field{-LABEL}$required$invalid</td>$n} ; | ||
| 270 | unless( $field{-HEADLINE} ) { | ||
| 271 | print qq{<td$valuestyle>} ; | ||
| 272 | print "<span$descstyle>$field{-DESC}</span><br />" if $field{-DESC} ; | ||
| 273 | delete @field{-LABEL,-VALIDATE,-CLEAN,-SIZE,-MAXLENGTH, | ||
| 274 | -ROWS,-COLUMNS,-COLSPAN} ; | ||
| 275 | no strict "refs" ; | ||
| 276 | local $^W = 0 ; # Switch off moans about undefined values. | ||
| 277 | print &{$type}( %field ) ; | ||
| 278 | # Prefer to say why immediately after the field rather than in a | ||
| 279 | # separate column. | ||
| 280 | print " $why" if $invalid and defined $why ; | ||
| 281 | print "</td>$n" ; | ||
| 282 | } | ||
| 283 | print "</tr>$n" if $endrow ; | ||
| 284 | } | ||
| 285 | |||
| 286 | print "</table>$n", ( ( $formref->{-STYLE_BUTTONS} eq 'center' ) ? | ||
| 287 | '<center>' : "<span$formref->{-STYLE_BUTTONS}>" ) ; | ||
| 288 | |||
| 289 | foreach my $fieldref ( @{$formref->{-BUTTONS}} ) { | ||
| 290 | if( $fieldref->{-DEFAULTS} ) { | ||
| 291 | print defaults( $fieldref->{-name} || 'Clear' ), " " ; | ||
| 292 | } | ||
| 293 | else { | ||
| 294 | print $n, submit( %$fieldref ), " " ; | ||
| 295 | } | ||
| 296 | } | ||
| 297 | |||
| 298 | print ( ( $formref->{-STYLE_BUTTONS} eq 'center' ) ? '</center>' : '</span>' ) ; | ||
| 299 | |||
| 300 | foreach my $fieldref ( @hidden ) { | ||
| 301 | my %field = %$fieldref ; | ||
| 302 | delete @field{-LABEL,-VALIDATE,-CLEAN,-SIZE,-MAXLENGTH,-ROWS,-COLUMNS, | ||
| 303 | -TYPE,-REQUIRED,-INVALID,-WHY} ; | ||
| 304 | print $n, hidden( %field ) ; | ||
| 305 | } | ||
| 306 | |||
| 307 | print $n, end_form, $n ; | ||
| 308 | |||
| 309 | if( $formref->{-FOOTER} ) { | ||
| 310 | if ( ref $formref->{-FOOTER} eq 'CODE' ) { | ||
| 311 | &{$formref->{-FOOTER}} ; | ||
| 312 | } | ||
| 313 | else { | ||
| 314 | print "$formref->{-FOOTER}$n" ; | ||
| 315 | } | ||
| 316 | } | ||
| 317 | else { | ||
| 318 | print hr, end_html ; | ||
| 319 | } | ||
| 320 | } | ||
| 321 | |||
| 322 | |||
| 323 | sub _on_valid_form { | ||
| 324 | # This is included for completeness - if you don't supply your own your | ||
| 325 | # form will simply throw away the user's data! | ||
| 326 | |||
| 327 | |||
| 328 | header, | ||
| 329 | start_html( 'Quick Form' ), | ||
| 330 | h3( 'Quick Form' ), | ||
| 331 | p( "You must define your own &on_valid_form subroutine, otherwise " . | ||
| 332 | "the data will simply be thrown away." ), | ||
| 333 | end_html, | ||
| 334 | ; | ||
| 335 | |||
| 336 | # If using pure mod_perl you should add: | ||
| 337 | # Apache::Constants::OK ; | ||
| 338 | # at the end of your on_valid_form routine and | ||
| 339 | # use Apache::Constants qw( :common ) ; | ||
| 340 | # along with the other 'use' commands in your program. | ||
| 341 | } | ||
| 342 | |||
| 343 | |||
| 344 | BEGIN { | ||
| 345 | |||
| 346 | %Translate = ( | ||
| 347 | 'cy' => { | ||
| 348 | -INTRO => "Cofnodwch y wybodaeth.", | ||
| 349 | -REQUIRED => "Mae angen llenwi'r adrannau sydd wedi eu clustnodi " . | ||
| 350 | "gyda ~R~.", | ||
| 351 | -INVALID => "Mae'r adrannau sydd wedi eu clustnodi gyda ~I~ " . | ||
| 352 | "yn cynnwys camgymeriadau neu yn wag.", | ||
| 353 | }, | ||
| 354 | 'de' => { | ||
| 355 | -INTRO => "Tragen Sie bitte die Informationen ein.", | ||
| 356 | -REQUIRED => "Bitte mindestens die mit ~R~ " . | ||
| 357 | "gekennzeichneten Felder ausfüllen.", | ||
| 358 | -INVALID => "Die mit ~I~ gekennzeichneten Felder " . | ||
| 359 | "enthalten Fehler oder sind leer.", | ||
| 360 | }, | ||
| 361 | 'en' => { | ||
| 362 | -INTRO => "Please enter the information.", | ||
| 363 | -REQUIRED => "Data entry fields marked with ~R~ are required.", | ||
| 364 | -INVALID => "Data entry fields marked with ~I~ contain errors " . | ||
| 365 | "or are empty.", | ||
| 366 | }, | ||
| 367 | 'es' => { | ||
| 368 | -INTRO => "Favor de introducir la información.", | ||
| 369 | -REQUIRED => "Los campos marcados con ~R~ son requeridos.", | ||
| 370 | -INVALID => "Los campos marcados con ~I~ contienen " . | ||
| 371 | "errores o están en blanco.", | ||
| 372 | }, | ||
| 373 | 'fr' => { | ||
| 374 | -INTRO => "Veuillez écrire l'information.", | ||
| 375 | -REQUIRED => "Des zones de saisie de données " . | ||
| 376 | "identifiées par " . | ||
| 377 | "~R~ sont exigées.", | ||
| 378 | -INVALID => "Des zones de saisie de données " . | ||
| 379 | "identifiées par ~I~ contenez les " . | ||
| 380 | "erreurs ou soyez vide.", | ||
| 381 | }, | ||
| 382 | 'he' => { | ||
| 383 | -INTRO => "ðà äæï ðúåðéí", | ||
| 384 | -REQUIRED => " ùãåú çåáä îñåîðéí á ~R~ ", | ||
| 385 | -INVALID => " ùãåú ùâåééí àå øé÷éí îñåîðéí á ~I~", | ||
| 386 | }, | ||
| 387 | ) ; | ||
| 388 | } | ||
| 389 | |||
| 390 | |||
| 391 | 1 ; | ||
| 392 | |||
| 393 | |||
| 394 | __END__ | ||